## Project: Registered Index-Linked Annuities in Qualified Retirement Plans
## Topic: UI Structure

library(shiny)
library(shinyjs)
library(scales)
library(DT)
library(tidyverse)
library(googlesheets4)
# setwd(app_directory)

gs4_auth(cache = ".secrets", email = "cellis212@gmail.com")
sheet_id <- "https://docs.google.com/spreadsheets/d/1f4-El8k2QPe7YOj2-z71CHo8ED8vWN0-Q9TFodWwHg4"

load(file = "./Data/Contracts.RData")
load(file = "./Data/logR.RData")
source(file = "./Functions.R")

reactiveConsole(enabled = TRUE)
# Testing
# load(file = "./Code/Data/Contracts.RData")
# load(file = "./Code/Data/logR.RData")
# source(file = "./Code/Functions.R")



# year_retire <- 2052 # RETIRE YEAR (i.e., "Target Date" in tables)






# ui  ---------------------------------------
ui <- fluidPage(
  
  titlePanel(""),
  # group_assigned <- sample(1:4, 1),
  group_assigned <- 4,
  gross_expense_ratio_random <- sample(2:length(cs$Cost_grid), 1),
  start_time <- Sys.time(),
  # gross_expense_ratio_random <- 4,
  navlistPanel(
    widths = c(2, 10),
    id = "barlist",
    
    # First welcome page: agree before proceed ---------------------------------------
    tabPanel(title = "(SURVEY MAY RESTART IF YOU CLICK ANY LINKS)", value = "Homepage",
             h3("Welcome!"),
             br(),
             strong("Important Technical Issues: Unfortunately you are not able to use the back button or click any of the links, otherwise the survey will resart."),
             br(),br(),
             p("Thank you for participating in this study. This form gives a brief preliminary overview of the study and provides contact information in case of any questions.  Please indicate that you agree with the conditions of the study by proceeding."),
             #br(),
             h3("Title of the Research Study:", strong("Registered Index-Linked Annuities in Qualified Retirement Plans")),
             br(),
             h5("Investigators:"),
             p("Cameron Ellis, Tippie College of Business, University of Iowa",
               br(),
               "Thorsten Moenig, Fox School of Business, Temple University",
               br(),
               "Jacqueline Volkman-Wise, Wharton School, University of Pennsylvania"),
             br(),
             h4("IRB Protocol Number:  #851987"),
             br(),
             p("This study involves research.  The purpose of the research is", strong("to investigate decision making in retirement investment situations."), "This study is being conducted by Cameron Ellis, Thorsten Moenig, and Jacqueline Volkman-Wise.  The study will be completed at the Wharton School, University of Pennsylvania (Philadelphia, Pennsylvania) and will be conducted online."),
             p("You must be 18 years or older to participate in this study."),
             p("If you agree to be in this study, you will be asked to do the following:",
               HTML("<ul><li>Complete a set of questions about retirement investment choices you are offered.
                  </li><li>Complete a set of questions about your background.</li></ul>")),
             p("Participation in this study will involve a total of", strong("45 minutes"), "of your time to be done correctly."),
             p("What you should know about a research study:",
               HTML("<ul><li>You volunteer to be in a research study.
                  </li><li>Whether you take part in this study is up to you.
                  </li><li>You can choose not to take part in the research study.
                  </li><li>You can agree to take part now and later change your mind.
                  </li><li> However, you will only be compensated if you finish the study. </li></ul>")
             ),
             p("Additional Information about this study:",
               HTML("<ul><li>There are no known risks associated with your participation in this research beyond those of everyday life.
                  </li><li>You will only be compensated for taking the survey once.
                  </li><li>If you have additional questions or wish to report a research-related problem, you may contact the primary investigator, Cameron Ellis, via email at Cameron-Ellis@uiowa.edu.
                  </li><li>If you have questions about your rights and welfare as a volunteer in the research study, please contact the Office of Regulatory Affairs at the University of Pennsylvania at (215) 662-4484.
                  </li><li>This research has been reviewed and approved by the University of Pennsylvania Institutional Review Board.
                  </li><li>Confidentiality of your research records will be strictly maintained by ensuring all data is kept secure, and only the primary investigator and the research team will have access to this data. This means that nobody else will have access to your data at any point during or after the study.</li></ul>")
             ),
             p("The alternative to participating is", strong("not to participate.")),
             br(),
             h5("By proceeding, you are agreeing to take part in this research study."),
             br(),
             actionButton('button_agree_survey', 'I Agree')
    ),
    
    "Before Start",
    # Additional Questions: age & salary ---------------------------------------
    tabPanel(title = "Demographic Questions", value = "Additional Questions",
             p("What is your Prolific Worker ID? (YOU MUST ENTER THIS TO GET CREDIT!!!)"),
             textInput("worker_ID", label = NULL),
             
             br(),
             p("What is your current age? If your age is not available, please select the closest option."),
             sliderInput("age_current", label = NULL, 
                         value = 25,
                         min = 18, 
                         max = 65),
             br(),
             p("At what age do you expect to retire? If your planned age is not available, please select the closest option."),
             sliderInput("age_retire", NULL, 
                         value = 65,
                         min = 45,
                         max = 85),
             br(),
             p("What is your current annual salary? If you are between jobs or you are a student, please enter your expected salary upon entering the workforce. If your income is not available, please select the closest option."),
             sliderInput("salary_current", NULL, 
                         value = 10000,
                         min = 10000,
                         max = 200000),
             br(),
             p("If you currently have a retirement plan, approximately how much money do you have in the plan now? If you do not know the exact number, please estimate it as best as you can and enter it below. If you have never had a retirement plan, please enter 0. If your account value is not available, please select the closest option."),
             sliderInput("retire_plan", NULL, 
                         value = 0,
                         min = 0,
                         max = 500000),
             br(),
             actionButton('button_to_q1', 'Confirm')
    ),
    
    
    # Second welcome page: background ---------------------------------------
    tabPanel(title = "Background", value = "Background",
             h3("Welcome!"),
             br(),
             p("Welcome to this study on choice behavior.  Thanks for your willingness to participate. In this study you are a company employee, and you will be asked to make an investment choice for your employer-provided retirement account.  You will need to choose among three investment options offered, each involving risk.  The options will be named Fund A, Fund B, and Fund C.  Figure 1 below shows how the choice options will be presented to you."),
             
             br(), 
             tableOutput("table_background"),
             br(),
             
             p("As Figure 1 shows, the investment options are indicated in rows. Each fund choice has information regarding the fund’s expense ratio, or management fee charged, and information on how the fund’s asset allocation will change over time."),
             p("In this example, the choices are called Target Date Funds. These funds take the complexity out of deciding which investment approach is right for you. They provide access to a broadly diversified portfolio within a single investment that automatically adjusts over time."),
             p("Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the fund's target allocations will gradually become more conservative."),
             p("For example, Fund A in Figure 1 above has a target date of 2060. Currently 93.5% of the fund assets are in equity funds and 6.5% of assets are in fixed-income funds.  As shown in the last column, by the target date, 2060, 50% of the fund will be invested in equity and 50% will be invested in fixed-income.  Equity funds, on average, have a long-term return of 8%.  Fixed-income funds, on average, have a long-term return of 3%."),
             p("In what follows, you will be given information about different fund types for investing your retirement money. You will be asked which fund you would like to select to invest your retirement money. You will be asked to confirm your choice after you select the fund you prefer."),
             br(),
             actionButton('button_to_practice_q1', 'Continue')
    ),
    
    # Practice Questions ---------------------------------------
    navbarMenu(title = "Practice Questions",
               # Practice Question 1 ---------------------------------------
               tabPanel(title = "Practice Question 1", value = "Practice Question 1",
                        h3("Practice Question 1: Target Date Funds"),
                        br(),
                        p("Some of the funds you will be offered as an investment choice are called “Target Date Funds” (TDFs). These funds take the complexity out of deciding which investment approach is right for you. They provide access to a broadly diversified portfolio within a single investment that automatically adjusts over time."),
                        p("Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity (riskier) and fixed-income (safer) index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                        p("Suppose you anticipate you will retire in 2060."), # CHECK ABOUT THE YEAR
                        p("You have a retirement plan offered to you by your employer.  Below are a set of target date funds with target dates that appropriately correspond to your anticipated year of retirement.  The table below shows information for each fund.",
                          HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.  
                  </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                  </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                  </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                        ),
                        
                        br(), 
                        tableOutput("table_practice_q1"), # CHECK THE "TARGET DATE"
                        br(),
                        
                        p(strong("You have a retirement plan offered to you by your employer. Which of the funds would you like to choose to invest your retirement money into?")),
                        br(),
                        radioButtons("choose_practice_q1", label = NULL,
                                     c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                        
                        br(),
                        actionButton('button_to_practice_q3', 'Confirm')
               ),
               
               # Practice Question 2 ---------------------------------------
               # tabPanel(title = "Practice Question 2", value = "Practice Question 2",
               #          h3("Practice Question 2: Target Date Funds"),
               #          br(),
               #          p("Suppose you anticipate you will retire in 2040."), # CHECK ABOUT THE YEAR
               #          p("You have a retirement plan offered to you by your employer. Below are a set of target date funds with target dates that appropriately correspond to your anticipated year of retirement."),
               #          p("The table below shows information for each fund.",
               #            HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.    
               #    </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age. 
               #    </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
               #    </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
               #          ),
               #          
               #          br(), 
               #          tableOutput("table_practice_q2"), # CHECK THE "TARGET DATE" 
               #          br(),
               #          
               #          p(strong("Recall the following Information about Target Date Funds:")),
               #          p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
               #          p(strong("You have a retirement plan offered to you by your employer. Which of the funds would you like to choose to invest your retirement money into?")),
               #          br(),
               #          radioButtons("choose_practice_q2", label = NULL,
               #                       c("Fund A", "Fund B", "Fund C"), selected = character(0)),
               #          
               #          br(),
               #          actionButton('button_to_practice_q3', 'Confirm')
               # ),
               
               # Practice Question 3 ---------------------------------------
               tabPanel(title = "Practice Question 2", value = "Practice Question 3",
                        h3("Practice Question 2: Registered Index-Linked Annuities (RILAs)"),
                        br(),
                        p("Another type of investment product is called a Registered Index-Linked Income (RILA). These investments have an account which receives a return based on the performance of a market index, such as the S&P 500. RILAS have a minimum guarantee on the return to help the investor have protection in cases of high negative returns on the associated index. The minimum guaranteed return is offered by the RILA having either a “floor” or “buffer,” which will be explained more below. In exchange for this guaranteed minimum return, there is also a cap on the highest return the RILA can earn. The minimum guaranteed return and cap are based on the returns on the index over one year."),
                        p("RILAs are becoming more popular as they provide the gains that can be offered through investment in equities, provide protection from overly high negative returns, and have lower fees or costs than other managed investment funds like Target Date Fund. "),
                        p(strong("RILAs with a Floor")),
                        p("One type of RILA has what is called a “floor” for the minimum guarantee on the return. The floor is the lowest return the RILA could earn. For example, suppose a RILA has a floor of -5% and this RILA is associated with the return earned on the S&P 500 index. If the actual return on the S&P 500 index is -10%, the return on the RILA would only be -5% since that is the lowest return the RILA can earn (or the maximum loss that could be obtained)."),
                        p("As mentioned earlier RILAs also have a cap, which is the maximum return the RILA can earn. Suppose the cap on a RILA is 8%. If the actual return on the index the RILA is associated with is 12%, the RILA would only earn a return of 8% since that is the cap and maximum return the RILA can earn."),
                        p(em(strong("Example:"),	"To better illustrate RILAs with a floor, consider the following example. Suppose you invest in a", strong("RILA that has a -10% floor and 8% cap."), "The table below shows the return the RILA would earn for different levels of actual returns on the index associated with the RILA.")),
                        
                        br(), 
                        tableOutput("table_practice_q3_floor"),
                        br(),
                        
                        p(strong("RILAs with a Buffer")),
                        p("Another type of RILA has what is called a “buffer” for downside protection, which limits the loss on the RILA when the return on the associated index is negative. That is, when the index associated with the RILA obtains a negative return, the return on the RILA is reduced by the buffer amount. For example, suppose a RILA has a buffer -5% and this RILA is associated with the return earned on the S&P 500 index. If the actual return on the S&P 500 index is -8%, the return on the RILA would only be -3%. Since the RILA has a buffer which offers protection of 5%, the return on the RILA equals the return on the associated index reduced by this buffer amount."),
                        p("Please note that if the buffer amount covers more than the negative return on the associated index, the return on the RILA will be zero. Buffers will not allow for positive returns on the RILA if the associated index had a negative return."),
                        p("As mentioned earlier RILAs also have a cap, which is the maximum return the RILA can earn. Suppose the cap on a RILA is 8%. If the actual return on the index the RILA is associated with is 12%, the RILA would only earn a return of 8% since that is the cap and maximum return the RILA can earn."),
                        p(em(strong("Example:"),	"To better illustrate RILAs with a buffer, consider the following example. Suppose you invest in a ", strong("RILA that has a -10% buffer and 8% cap."), "The table below shows the return the RILA would earn for different levels of actual returns on the index associated with the RILA.")),
                        
                        br(), 
                        tableOutput("table_practice_q3_buffer"),
                        br(),
                        
                        p("Below are some practice questions to ensure you understand how a floor and buffer works for RILAs."),
                        br(),
                        h4("Practice Questions for RILA with Floor"),
                        p("Suppose you invest in a RILA that has a floor of -15% and a cap of 10%. Please answer the following questions."),
                        p(strong("1. The index return is 12%. What is the return you earn on the RILA?")),
                        radioButtons("choose_practice_q3_1", label = NULL,
                                     c("0%", "10%", "12%", "15%", "27%"),
                                     selected = character(0)),
                        p(strong("2. The index return is -20%.  What is the return you earn on the RILA?")),
                        radioButtons("choose_practice_q3_2", label = NULL,
                                     c("0%", "10%", "-5%", "-15%", "-20%"),selected = character(0)),
                        br(),
                        h4("Practice Questions for RILA with Buffer"),
                        p("Suppose you invest in a RILA that has a buffer of -10% and a cap of 12%.  Please answer the following questions."),
                        p(strong("1. The index return is 10%.  What is the return you earn on the RILA?")),
                        radioButtons("choose_practice_q3_3", label = NULL,
                                     c("0%", "2%", "10%", "12%", "22%"),selected = character(0)),
                        p(strong("2. The index return is -15%.  What is the return you earn on the RILA?")),
                        radioButtons("choose_practice_q3_4", label = NULL,
                                     c("0%", "-3%", "-5%", "-15%", "-25%"),selected = character(0)),
                        br(),
                        actionButton('button_to_practice_q3a', 'Confirm')
               ),
               
               # Practice Question 3 Answers ---------------------------------------
               tabPanel(title = "Practice Question 2 Answers", value = "Practice Question 3 Answers",
                        h3("Practice Question 2 Answers: Registered Index-Linked Annuities (RILAs)"),
                        br(),
                        
                        h4("Practice Questions for RILA with Floor"),
                        p("Suppose you invest in a RILA that has a floor of -15% and a cap of 10%. Please answer the following questions."),
                        p("1. The index return is 12%. What is the return you earn on the RILA?"),
                        p(strong("The correct answer is 10%. The cap prevents the return from being higher.")),
                        br(),
                        p("2. The index return is -20%.  What is the return you earn on the RILA?"),
                        p(strong("The correct answer is -15%.  The floor prevents you from losing the full 20%.")),
                        
                        br(),
                        h4("Practice Questions for RILA with Buffer"),
                        p("Suppose you invest in a RILA that has a buffer of -10% and a cap of 12%.  Please answer the following questions."),
                        p("1. The index return is 10%.  What is the return you earn on the RILA?"),
                        p(strong("The correct answer is 10%.  The cap is higher than the return so you earn the full amount.")),
                        
                        
                        br(),
                        p("2. The index return is -15%.  What is the return you earn on the RILA?"),
                        p(strong("The correct answer is -5%.  The buffer stops the first 10% of losses.")),
                        
                        br(),
                        actionButton('button_to_practice_q4', 'Confirm')
               ),
               
               # Practice Question 4 ---------------------------------------
               tabPanel(title = "Practice Question 3", value = "Practice Question 4",
                        h3("Practice Question 3: Target Date Registered Index-Linked Annuities (Target Date RILAs)"),
                        br(),
                        p("Some of the funds you will be offered as an investment choice are called “Target Date Registered Index-Linked Annuities” (Target Date RILAs). This investment takes the complexity out of deciding which investment approach is right for you. Similar to Target Date Funds, Target Date RILAs provide access to equity markets and automatically adjust the level of risk exposure over time."),
                        p("The target date is the approximate date when you expect to begin withdrawing money. As you near your target date,the buffer becomes larger (or the floor becomes less negative), thereby increasing your protection against negative outcomes. Additionally, the cap rate will be reduced accordingly as there will be less opportunity for large gains. The automatic adjustment in floors (or buffers) and cap rates creates a similar dynamic risk profile to Target Date Funds."),
                        p("Suppose you anticipate you will retire in 2050."), # CHECK OUT THE YEAR
                        p("You have a retirement plan offered to you by your employer. Below are a set of Target Date RILAs with target dates that appropriately correspond to your anticipated year of retirement. The Target Date RILAs shown below all have floors."),
                        p("The table below shows information for each investment.",
                          HTML("<ul><li>The “Implied Gross Expense Ratio” indicates the annual cost for the investment implied by its cap rate. This number has been estimated to help you determine the implied cost of the product.   
                  </li><li>The three columns under the “Floor / Cap Level” heading indicate how the floor and cap levels for the RILA changes over time. That is, it shows what the floor and cap will be for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is at your retirement age. 
                  </li><li>Recall that for RILAs with a floor:
                  <ul><li>The floor is the guaranteed minimum return that can be earned.
                  </li><li>The cap is the maximum return that can be earned.</li></ul>
                  </li><li>Recall that equity funds, on average, have a long-term return of 8%.
                  </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                        ),
                        
                        br(), 
                        tableOutput("table_practice_q4"), #CHECK THE "TARGET DATE"
                        # NOTES BY JACKIE: Cap rate is selected by program based on what floor level is. Note:  floor and cap for fund A chosen to be somewhat in line with fund A for TDF
                        br(),
                        
                        p(strong("Recall the following Information about Target Date RILAs with a Floor:")),
                        p("The floor offers a minimum guarantee on the return. The floor is the lowest return the RILA could earn. For example, suppose a RILA has a floor of -5% and this RILA is associated with the return earned on the S&P 500 index. If the actual return on the S&P 500 index is -10%, the return on the RILA would only be -5% since that is the lowest return the RILA can earn (or the maximum loss that could be obtained)."),
                        p("As mentioned earlier RILAs also have a cap, which is the maximum return the RILA can earn. Suppose the cap on a RILA is 8%. If the actual return on the index the RILA is associated with is 12%, the RILA would only earn a return of 8% since that is the cap and maximum return the RILA can earn."),
                        p(strong("You have a retirement plan offered to you by your employer. Which of the funds would you like to choose to invest your retirement money into?")),
                        br(),
                        radioButtons("choose_practice_q4", label = NULL,
                                     c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                        
                        br(),
                        actionButton('button_to_practice_q5', 'Confirm')
               ),
               
               # Practice Question 5 ---------------------------------------
               tabPanel(title = "Practice Question 4", value = "Practice Question 5",
                        h3("Practice Question 4: Target Date Registered Index-Linked Annuities (Target Date RILAs)"),
                        br(),
                        p("Suppose you anticipate you will retire in 2050."), # CHECK OUT THE YEAR
                        p("You have a retirement plan offered to you by your employer. Below are a set of Target Date RILAs with target dates that appropriately correspond to your anticipated year of retirement. The Target Date RILAs shown below all have buffers."),
                        p("The table below shows information for each investment.",
                          HTML("<ul><li>The “Implied Gross Expense Ratio” indicates the annual cost for the investment implied by its cap rate.  This number has been estimated to help you determine the implied cost of the product.  
                  </li><li>The three columns under the “Buffer / Cap Level” heading indicate how the buffer and cap levels for the RILA changes over time. That is, it shows what the buffer and cap will be for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is at your retirement age.
                  </li><li>Recall that for RILAs with a buffer:
                  <ul><li>The buffer is the amount that limits losses when the return on the associated index is negative. That is, the buffer is the amount by which the negative return on the index is reduced by to determine the return earned on the RILA.
                  </li><li>The cap is the maximum return that can be earned.</li></ul>
                  </li><li>Recall that equity funds, on average, have a long-term return of 8%.  
                  </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                        ),
                        
                        br(), 
                        tableOutput("table_practice_q5"), # CHECK THE "TARGET DATE"
                        # NOTES BY JACKIE: Cap rate is selected by program based on what floor level is.
                        br(),
                        
                        p(strong("Recall the following Information about Target Date RILAs with a Buffer:")),
                        p("The buffer limits the loss on the RILA when the return on the associated index is negative. That is, when the index associated with the RILA obtains a negative return, the return on the RILA is reduced by the buffer amount. For example, suppose a RILA has a buffer -5% and this RILA is associated with the return earned on the S&P 500 index. If the actual return on the S&P 500 index is -8%, the return on the RILA would only be -3%. Since the RILA has a buffer which offers protection of -5%, the return on the RILA equals the return on the associated index reduced by this buffer amount."),
                        p("Please note that if the buffer amount covers more than the negative return on the associated index, the return on the RILA will be zero. Buffers will not allow for positive returns on the RILA if the associated index had a negative return."),
                        p("As mentioned earlier RILAs also have a cap, which is the maximum return the RILA can earn. Suppose the cap on a RILA is 8%. If the actual return on the index the RILA is associated with is 12%, the RILA would only earn a return of 8% since that is the cap and maximum return the RILA can earn."),
                        p("Target Date RILAs provide access to equity markets and automatically adjust the level of risk exposure over time. The target date is the approximate date when you expect to begin withdrawing money. As you near your target date,the buffer becomes larger (or the floor becomes less negative), thereby increasing your protection against negative outcomes. Additionally, the cap rate will be reduced accordingly as there will be less opportunity for large gains. The automatic adjustment in floors (or buffers) and cap rates creates a similar dynamic risk profile to Target Date Fund."),
                        p(strong("You have a retirement plan offered to you by your employer. Which of the funds would you like to choose to invest your retirement money into?")),
                        br(),
                        radioButtons("choose_practice_q5", label = NULL,
                                     c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                        
                        br(),
                        actionButton('button_to_confirm_instruction', 'Confirm')
               )
    ),
    
    # Understand the instruction and confirm participation ---------------------------------------
    tabPanel(title = "Confirm the Participation", value = "Confirm the Participation",
             h4("Do you understand fully the instructions of this study? If you understand and are ready to proceed to the first part of the study, please check the “I understand and am ready to proceed” box and click “Continue” at the bottom of the screen."),
             br(),
             checkboxInput("checkbox_understandsurvey", "I understand and am ready to proceed.", value = NULL),
             br(),
             actionButton('button_to_additional_info', 'Continue')
    ),
    
    
    # Begin the Survey ---------------------------------------
    "Begin the Survey",
    
    # Survey Questions ---------------------------------------
    if (group_assigned == 1) {
      
      # Group 1 ---------------------------------------
      navbarMenu(title = "Survey Questions",
                 
                 # Group 1 Question 1 ---------------------------------------
                 tabPanel(title = "Question 1", value = "Group 1 Question 1",
                          tabsetPanel(
                            id = "barlist_g1_q1",
                            
                            tabPanel(title = strong("Question 1-A"), value = "Group 1 Question 1-A",
                                     br(),
                                     p("Based on the information you gave in the prior questions you anticipate you will retire in the year ", textOutput("year_retire_g1_q1_a", inline = TRUE)),
                                     p("You have a retirement plan offered to you by your employer. Below are a set of target date funds with target dates that appropriately correspond to your anticipated year of retirement."),
                                     p("The table below shows information for each fund.",
                                       HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.   
                                  </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                  </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                  </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     ),
                                     p(strong("The table may take up to a minute to load")),
                                     
                                     br(), 
                                     tableOutput("table_g1_q1_a"),
                                     br(),
                                     
                                     p(strong("Recall this Information about Target Date Funds:")),
                                     p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("You have a retirement plan offered to you by your employer. Which of the funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g1_q1_a", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g1_q1_b', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 1-B"), value = "Group 1 Question 1-B",
                                     br(),
                                     # p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g1_q1_b", inline = TRUE)),
                                     p("The prices for the plans offered to you by your employer have changed. Below are the new prices."),
                                     # p("The table below shows information for each fund.",
                                     #      HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.     
                                     # </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                     # </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                     # </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     #    ),
                                     
                                     br(), 
                                     tableOutput("table_g1_q1_b"),
                                     br(),
                                     
                                     # p(strong("Recall this Information about Target Date Funds:")),
                                     # p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Which of these funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g1_q1_b", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g1_q1_c', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 1-C"), value = "Group 1 Question 1-C1",
                                     br(),
                                     p("In the previous question, the gross expense ratio had been increased for the fund you had chosen earlier. As a result, you chose a different fund to invest your retirement funds."),
                                     p("What is the lowest gross expense ratio on your originally selected fund where you would not have switched your fund choice?"),
                                     uiOutput("slider_g1_q1_c1"),
                                     
                                     
                                     br(),
                                     actionButton('button_c1_to_g1_q2', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 1-C"), value = "Group 1 Question 1-C2",
                                     br(),
                                     p("In the previous question, the gross expense ratio had been increased for the fund you had chosen earlier. You continued to choose the fund that you had originally selected."),
                                     p("What is the highest gross expense ratio on your originally selected fund where you would have switched your fund choice?"),
                                     uiOutput("slider_g1_q1_c2"),
                                     
                                     br(),
                                     p(strong("To which fund would you have switched?")),
                                     br(),
                                     radioButtons("choose_g1_q1_c2", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_c2_to_g1_q2', 'Confirm')
                            )
                          )
                 ),
                 
                 # Group 1 Question 2 ---------------------------------------
                 tabPanel(title = "Question 2", value = "Group 1 Question 2",
                          tabsetPanel(
                            id = "barlist_g1_q2",
                            
                            tabPanel(title = strong("Question 2-A"), value = "Group 1 Question 2-A",
                                     br(),
                                     p("Based on the information you gave in prior questions you anticipate you will retire in the year", textOutput("year_retire_g1_q2_a")),
                                     p("You have a retirement plan offered to you by your employer. Below are a set of Target Date RILAs with target dates that appropriately correspond to your anticipated year of retirement. The Target Date RILAs shown below all have floors."),
                                     p("The table below shows information for each investment.",
                                       HTML("<ul><li>The “Implied Gross Expense Ratio” indicates the annual cost for the investment implied by its cap rate. This number has been estimated to help you determine the implied cost of the product.    
                                  </li><li>The three columns under the “Floor / Cap Level” heading indicate how the floor and cap levels for the RILA changes over time. That is, it shows what the floor and cap will be for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is at your retirement age.
                                  </li><li>Recall that for RILAs with a floor:
                                  <ul><li>The floor is the guaranteed minimum return that can be earned.
                                  </li><li>The cap is the maximum return that can be earned.</li></ul>
                                  </li><li>Recall that equity funds, on average, have a long-term return of 8%.    
                                  </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     ),
                                     p(strong("The table may take up to a minute to load")),
                                     
                                     br(), 
                                     tableOutput("table_g1_q2_a"),
                                     br(),
                                     
                                     p(strong("Recall this information about Target Date RILAs with Floors:")),
                                     p("The floor offers a minimum guarantee on the return. The floor is the lowest return the RILA could earn. For example, suppose a RILA has a floor of -5% and this RILA is associated with the return earned on the S&P 500 index. If the actual return on the S&P 500 index is -10%, the return on the RILA would only be -5% since that is the lowest return the RILA can earn (or the maximum loss that could be obtained)."),
                                     p("As mentioned earlier RILAs also have a cap, which is the maximum return the RILA can earn. Suppose the cap on a RILA is 8%. If the actual return on the index the RILA is associated with is 12%, the RILA would only earn a return of 8% since that is the cap and maximum return the RILA can earn."),
                                     p("Target Date RILAs provide access to equity markets and automatically adjust the level of risk exposure over time. The target date is the approximate date when you expect to begin withdrawing money. As you near your target date,the buffer becomes larger (or the floor becomes less negative), thereby increasing your protection against negative outcomes. Additionally, the cap rate will be reduced accordingly as there will be less opportunity for large gains. The automatic adjustment in floors (or buffers) and cap rates creates a similar dynamic risk profile to Target Date Funds."),
                                     p(strong("You have a retirement plan offered to you by your employer. Which of the funds below would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g1_q2_a", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g1_q2_b', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 2-B"), value = "Group 1 Question 2-B",
                                     br(),
                                     # p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g1_q1_b", inline = TRUE)),
                                     p("The prices for the plans offered to you by your employer have changed. Below are the new prices."),
                                     # p("The table below shows information for each fund.",
                                     #      HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.     
                                     # </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                     # </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                     # </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     #    ),
                                     
                                     br(), 
                                     tableOutput("table_g1_q2_b"),
                                     br(),
                                     
                                     # p(strong("Recall this Information about Target Date Funds:")),
                                     # p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Which of these funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g1_q2_b", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g1_q2_c', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 2-C"), value = "Group 1 Question 2-C1",
                                     br(),
                                     p("In the previous question, the implied gross expense ratio had been increased for the fund you had chosen earlier. As a result, you chose a different fund to invest your retirement funds.  "),
                                     p("What is the lowest gross expense ratio on your originally selected fund where you would not have switched your fund choice?"),
                                     uiOutput("slider_g1_q2_c1"),
                                     br(),
                                     actionButton('button_c1_to_g1_q3', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 2-C"), value = "Group 1 Question 2-C2",
                                     br(),
                                     p("In the previous question, the implied gross expense ratio had been increased for the fund you had chosen earlier. You continued to choose the fund that you had originally selected.  "),
                                     p("What is the highest gross expense ratio on your originally selected fund where you would have switched your fund choice?"),
                                     uiOutput("slider_g1_q2_c2"),
                                     br(),
                                     p(strong("To which fund would you have switched?")),
                                     br(),
                                     radioButtons("choose_g1_q2_c2", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_c2_to_g1_q3', 'Confirm')
                            )
                          )
                 ),
                 
                 # Group 1 Question 3 ---------------------------------------
                 tabPanel(title = "Question 3", value = "Group 1 Question 3",
                          tabsetPanel(
                            id = "barlist_g1_q3",
                            
                            tabPanel(title = strong("Question 3-A"), value = "Group 1 Question 3-A",
                                     br(),
                                     p("Based on the information you gave in prior questions you anticipate you will retire in the year", textOutput("year_retire_g1_q3_a", inline = TRUE)),
                                     p("You have a retirement plan offered to you by your employer. Below are a set of Target Date RILAs with target dates that appropriately correspond to your anticipated year of retirement. The Target Date RILAs shown below all have buffers."),
                                     p("The table below shows information for each investment. ",
                                       HTML("<ul><li>The “Implied Gross Expense Ratio” indicates the annual cost for the investment implied by its cap rate. This number has been estimated to help you determine the implied cost of the product.      
                                  </li><li>The three columns under the “Buffer / Cap Level” heading indicate how the buffer and cap levels for the RILA changes over time. That is, it shows what the buffer and cap will be for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is at your retirement age.
                                  </li><li>Recall that for RILAs with a buffer:
                                  <ul><li>The buffer is the amount that limits losses when the return on the associated index is negative. That is, the buffer is the amount by which the negative return on the index is reduced by to determine the return earned on the RILA.
                                  </li><li>The cap is the maximum return that can be earned.</li></ul>
                                  </li><li>Recall that equity funds, on average, have a long-term return of 8%.
                                  </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     ),
                                     p(strong("The table may take up to a minute to load")),
                                     
                                     br(), 
                                     tableOutput("table_g1_q3_a"),
                                     br(),
                                     
                                     p(strong("Recall the following Information about Target Date RILAs with a Buffer:")),
                                     p("The buffer limits the loss on the RILA when the return on the associated index is negative. That is, when the index associated with the RILA obtains a negative return, the return on the RILA is reduced by the buffer amount. For example, suppose a RILA has a buffer -5% and this RILA is associated with the return earned on the S&P 500 index. If the actual return on the S&P 500 index is -8%, the return on the RILA would only be -3%. Since the RILA has a buffer which offers protection of -5%, the return on the RILA equals the return on the associated index reduced by this buffer amount."),
                                     p("Please note that if the buffer amount covers more than the negative return on the associated index, the return on the RILA will be zero. Buffers will not allow for positive returns on the RILA if the associated index had a negative return."),
                                     p("As mentioned earlier RILAs also have a cap, which is the maximum return the RILA can earn. Suppose the cap on a RILA is 8%. If the actual return on the index the RILA is associated with is 12%, the RILA would only earn a return of 8% since that is the cap and maximum return the RILA can earn."),
                                     p("Target Date RILAs provide access to equity markets and automatically adjust the level of risk exposure over time. The target date is the approximate date when you expect to begin withdrawing money. As you near your target date,the buffer becomes larger (or the floor becomes less negative), thereby increasing your protection against negative outcomes. Additionally, the cap rate will be reduced accordingly as there will be less opportunity for large gains. The automatic adjustment in floors (or buffers) and cap rates creates a similar dynamic risk profile to Target Date Funds."),
                                     p(strong("You have a retirement plan offered to you by your employer. Which of the funds below would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g1_q3_a", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g1_q3_b', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 3-B"), value = "Group 1 Question 3-B",
                                     br(),
                                     # p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g1_q1_b", inline = TRUE)),
                                     p("The prices for the plans offered to you by your employer have changed. Below are the new prices."),
                                     # p("The table below shows information for each fund.",
                                     #      HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.     
                                     # </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                     # </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                     # </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     #    ),
                                     
                                     br(), 
                                     tableOutput("table_g1_q3_b"),
                                     br(),
                                     
                                     # p(strong("Recall this Information about Target Date Funds:")),
                                     # p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Which of these funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g1_q3_b", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g1_q3_c', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 3-C"), value = "Group 1 Question 3-C1",
                                     br(),
                                     p("In the previous question, the implied gross expense ratio had been increased for the fund you had chosen earlier. As a result, you chose a different fund to invest your retirement funds."),
                                     p("What is the lowest gross expense ratio on your originally selected fund where you would not have switched your fund choice?"),
                                     uiOutput("slider_g1_q3_c1"),
                                     br(),
                                     actionButton('button_c1_to_g1_q4', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 3-C"), value = "Group 1 Question 3-C2",
                                     br(),
                                     p("In the previous question, the implied gross expense ratio had been increased for the fund you had chosen earlier. You continued to choose the fund that you had originally selected."),
                                     p("What is the highest gross expense ratio on your originally selected fund where you would have switched your fund choice?"),
                                     uiOutput("slider_g1_q3_c2"),
                                     br(),
                                     p(strong("To which fund would you have switched?")),
                                     br(),
                                     radioButtons("choose_g1_q3_c2", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_c2_to_g1_q4', 'Confirm')
                            )
                          )
                 ),
                 
                 # Group 1 Question 4 ---------------------------------------
                 tabPanel(title = "Question 4", value = "Group 1 Question 4",
                          tabsetPanel(
                            id = "barlist_g1_q4",
                            
                            tabPanel(title = strong("Question 4-A"), value = "Group 1 Question 4-A",
                                     br(),
                                     p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g1_q4_a", inline = TRUE)),
                                     p("You have a retirement plan offered to you by your employer. Below are three funds which you can select, all with target dates that appropriately correspond to your anticipated year of retirement. One fund is a Target Date Fund. The second fund is a Target Date RILA with a floor and the third fund is a Target Date RILA with a buffer."),
                                     p("The tables below show information for each fund.",
                                       HTML("<ul><li>For the Target Date Fund:
                                  <ul><li>The Expense Ratio indicates the annual fee that will be charged for managing the fund.
                                  </li><li>The three columns under the “Target Allocation” heading for the Target Date Fund indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.</li></ul>
                                  </li><li>For the Target Date RILAs:
                                  <ul><li>The “Implied Gross Expense Ratio” indicates the annual cost for the investment implied by its cap rate. This number has been estimated to help you determine the implied cost of the product.  
                                  </li><li>The floor is the guaranteed minimum return that can be earned.
                                  </li><li>The buffer is the amount that limits losses when the return on the associated index is negative. That is, the buffer is the amount by which the negative return on the index is reduced by to determine the return earned on the RILA.
                                  </li><li>The cap is the maximum return that can be earned.</li></ul>
                                  </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                  </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     ),
                                     p(strong("The table may take up to a minute to load")),
                                     
                                     br(), 
                                     tableOutput("table_g1_q4_a1"),
                                     tableOutput("table_g1_q4_a2"),
                                     tableOutput("table_g1_q4_a3"),
                                     br(),
                                     
                                     p(strong("Recall this Information about Target Date Funds:")),
                                     p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Recall the following Information about Target Date RILAs:")),
                                     p("Target Date RILAs provide access to equity markets and automatically adjust the level of risk exposure over time. The target date is the approximate date when you expect to begin withdrawing money. As you near your target date,the buffer becomes larger (or the floor becomes less negative), thereby increasing your protection against negative outcomes. Additionally, the cap rate will be reduced accordingly as there will be less opportunity for large gains. The automatic adjustment in floors (or buffers) and cap rates creates a similar dynamic risk profile to Target Date Funds."),
                                     p(HTML("<ul><li><b>Floor</b>: The floor offers a minimum guarantee on the return. The floor is the lowest return the RILA could earn. For example, suppose a RILA has a floor of -5% and this RILA is associated with the return earned on the S&P 500 index. If the actual return on the S&P 500 index is -10%, the return on the RILA would only be -5% since that is the lowest return the RILA can earn (or the maximum loss that could be obtained).")),
                                     p(HTML("</li><li><b>Buffer</b>: The buffer limits the loss on the RILA when the return on the associated index is negative. That is, when the index associated with the RILA obtains a negative return, the return on the RILA is reduced by the buffer amount. For example, suppose a RILA has a buffer -5% and this RILA is associated with the return earned on the S&P 500 index. If the actual return on the S&P 500 index is -8%, the return on the RILA would only be -3%. Since the RILA has a buffer which offers protection of -5%, the return on the RILA equals the return on the associated index reduced by this buffer amount.")),
                                     p(HTML("</li><li><b>Cap</b>: The cap is the maximum return the RILA can earn. Suppose the cap on a RILA is 8%. If the actual return on the index the RILA is associated with is 12%, the RILA would only earn a return of 8% since that is the cap and maximum return the RILA can earn.</li></ul>")),
                                     p(strong("You have a retirement plan offered to you by your employer. Which of the funds below would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g1_q4_a", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g1_q4_b', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 4-B"), value = "Group 1 Question 4-B",
                                     br(),
                                     # p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g1_q1_b", inline = TRUE)),
                                     p("The prices for the plans offered to you by your employer have changed. Below are the new prices."),
                                     # p("The table below shows information for each fund.",
                                     #      HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.     
                                     # </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                     # </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                     # </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     #    ),
                                     
                                     br(), 
                                     tableOutput("table_g1_q4_b1"),
                                     tableOutput("table_g1_q4_b2"),
                                     tableOutput("table_g1_q4_b3"),
                                     br(),
                                     
                                     # p(strong("Recall this Information about Target Date Funds:")),
                                     # p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Which of these funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g1_q4_b", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g1_q4_c', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 4-C"), value = "Group 1 Question 4-C1",
                                     br(),
                                     p("In the previous question, the gross expense ratio or implied gross ratio had been increased for the fund you had chosen earlier. As a result, you chose a different fund to invest your retirement funds.  "),
                                     p("What is the lowest gross expense ratio on your originally selected fund where you would not have switched your fund choice?"),
                                     uiOutput("slider_g1_q4_c1"),
                                     br(),
                                     actionButton('button_g1_c1_to_background_info', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 4-C"), value = "Group 1 Question 4-C2",
                                     br(),
                                     p("In the previous question, the gross expense ratio or implied gross expense ratio had been increased for the fund you had chosen earlier. You continued to choose the fund that you had originally selected.  "),
                                     p("What is the highest gross expense ratio on your originally selected fund where you would have switched your fund choice?"),
                                     uiOutput("slider_g1_q4_c2"),
                                     br(),
                                     p(strong("To which fund would you have switched?")),
                                     br(),
                                     radioButtons("choose_g1_q4_c2", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_g1_c2_to_background_info', 'Confirm')
                            )
                          )
                 )
      )
      
    } else if (group_assigned == 2) {
      
      # Group 2 ---------------------------------------
      navbarMenu(title = "Survey Questions",
                 
                 # Group 2 Question 1 ---------------------------------------
                 tabPanel(title = "Question 1", value = "Group 2 Question 1",
                          tabsetPanel(
                            id = "barlist_g2_q1",
                            
                            tabPanel(title = strong("Question 1-A"), value = "Group 2 Question 1-A",
                                     br(),
                                     p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g2_q1_a", inline = TRUE)),
                                     p("You have a retirement plan offered to you by your employer. Below are a set of target date funds with target dates that appropriately correspond to your anticipated year of retirement. The first table shows the target date fund your employer already chose for your investments. The second table shows other target date funds that are available for you to invest your retirement money."),
                                     p("The tables below show information for each fund option offered.",
                                       HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.   
                                            </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time.  That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                            </li><li>Equity funds, on average, have a long-term return of 8%.  Fixed-income funds, on average, have a long-term return of 3%.
                                            </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     ),
                                     p("Your employer has already chosen the following target date fund to invest retirement money for employees that do not actively choose an investment."),
                                     
                                     p(strong("The table may take up to a minute to load")),
                                     br(), 
                                     tableOutput("table_g2_q1_a1"),
                                     br(),
                                     
                                     p("You also have the choice to invest in one of the following funds if you would like:"),
                                     
                                     br(), 
                                     tableOutput("table_g2_q1_a2"),
                                     br(),
                                     
                                     p(strong("Recall this Information about Target Date Funds:")),
                                     p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("You have a retirement plan offered to you by your employer. Which of the funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g2_q1_a", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g2_q1_b', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 1-B"), value = "Group 2 Question 1-B",
                                     br(),
                                     # p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g2_q1_b", inline = TRUE)),
                                     p("The prices for the plans offered to you by your employer have changed. Below are the new prices."),
                                     # p("The table below shows information for each fund.",
                                     #      HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.     
                                     # </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                     # </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                     # </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     #    ),
                                     br(), 
                                     tableOutput("table_g2_q1_b1"),
                                     br(),
                                     
                                     p("You also have the choice to invest in one of the following funds if you would like:"),
                                     
                                     br(), 
                                     tableOutput("table_g2_q1_b2"),
                                     br(),
                                     
                                     # p(strong("Recall this Information about Target Date Funds:")),
                                     # p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Which of these funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g2_q1_b", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g2_q1_c', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 1-C"), value = "Group 2 Question 1-C1",
                                     br(),
                                     p("In the previous question, the gross expense ratio had been increased for the fund you had chosen earlier. As a result, you chose a different fund to invest your retirement funds. "),
                                     p("What is the lowest gross expense ratio on your originally selected fund where you would not have switched your fund choice?  "),
                                     uiOutput("slider_g2_q1_c1"),
                                     br(),
                                     actionButton('button_c1_to_g2_q2', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 1-C"), value = "Group 2 Question 1-C2",
                                     br(),
                                     p("In the previous question, the gross expense ratio had been increased for the fund you had chosen earlier.  You continued to choose the fund that you had originally selected. "),
                                     p("What is the highest gross expense ratio on your originally selected fund where you would have switched your fund choice?  "),
                                     uiOutput("slider_g2_q1_c2"),
                                     br(),
                                     p(strong("To which fund would you have switched? ")),
                                     br(),
                                     radioButtons("choose_g2_q1_c2", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_c2_to_g2_q2', 'Confirm')
                            )
                          )
                 ),
                 
                 # Group 2 Question 2 ---------------------------------------
                 tabPanel(title = "Question 2", value = "Group 2 Question 2",
                          tabsetPanel(
                            id = "barlist_g2_q2",
                            
                            tabPanel(title = strong("Question 2-A"), value = "Group 2 Question 2-A",
                                     br(),
                                     p("Based on the information you gave in prior questions you anticipate you will retire in the year", textOutput("year_retire_g2_q2_a", inline = TRUE)),
                                     p("You have a retirement plan offered to you by your employer. Below are a set of Target Date RILAs with target dates that appropriately correspond to your anticipated year of retirement.  The Target Date RILAs shown below all have floors."),
                                     p("The tables below show information for each investment.",
                                       HTML("<ul><li>The “Implied Gross Expense Ratio” indicates the annual cost for the investment implied by its cap rate. This number has been estimated to help you determine the implied cost of the product.  
                                             </li><li>The three columns under the “Floor / Cap Level” heading indicate how the floor and cap levels for the RILA changes over time. That is, it shows what the floor and cap will be for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is at your retirement age. 
                                             </li><li>Recall that for RILAs with a floor:
                                                  <ul><li>The floor is the guaranteed minimum return that can be earned.
                                                  </li><li>The cap is the maximum return that can be earned.</li></ul>
                                             </li><li>Recall that equity funds, on average, have a long-term return of 8%.    
                                             </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     ),
                                     p("Your employer has already chosen the following Target Date RILA to invest retirement money for employees that do not actively choose an investment."),
                                     
                                     p(strong("The table may take up to a minute to load")),
                                     br(), 
                                     tableOutput("table_g2_q2_a1"),
                                     br(),
                                     
                                     p("You also have the choice to invest in one of the following investments if you would like:"),
                                     
                                     br(), 
                                     tableOutput("table_g2_q2_a2"),
                                     br(),
                                     
                                     p(strong("Recall this information about Target Date RILAs with Floors:")),
                                     p("The floor offers a minimum guarantee on the return.  The floor is the lowest return the RILA could earn.  For example, suppose a RILA has a floor of -5% and this RILA is associated with the return earned on the S&P 500 index.  If the actual return on the S&P 500 index is -10%, the return on the RILA would only be -5% since that is the lowest return the RILA can earn (or the maximum loss that could be obtained)."),
                                     p("As mentioned earlier RILAs also have a cap, which is the maximum return the RILA can earn.  Suppose the cap on a RILA is 8%.  If the actual return on the index the RILA is associated with is 12%, the RILA would only earn a return of 8% since that is the cap and maximum return the RILA can earn."),
                                     p("Target Date RILAs provide access to equity markets and automatically adjust the level of risk exposure over time. The target date is the approximate date when you expect to begin withdrawing money. As you near your target date,the buffer becomes larger (or the floor becomes less negative), thereby increasing your protection against negative outcomes.  Additionally, the cap rate will be reduced accordingly as there will be less opportunity for large gains. The automatic adjustment in floors (or buffers) and cap rates creates a similar dynamic risk profile to Target Date Funds."),
                                     p(strong("You have a retirement plan offered to you by your employer.  Which of the funds below would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g2_q2_a", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g2_q2_b', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 2-B"), value = "Group 2 Question 2-B",
                                     br(),
                                     # p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g2_q1_b", inline = TRUE)),
                                     p("The prices for the plans offered to you by your employer have changed. Below are the new prices."),
                                     # p("The table below shows information for each fund.",
                                     #      HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.     
                                     # </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                     # </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                     # </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     #    ),
                                     p("Your employer has already chosen the following Target Date RILA to invest retirement money for employees that do not actively choose an investment."),
                                     
                                     br(), 
                                     tableOutput("table_g2_q2_b1"),
                                     br(),
                                     
                                     p("You also have the choice to invest in one of the following investments if you would like:"),
                                     
                                     br(), 
                                     tableOutput("table_g2_q2_b2"),
                                     br(),
                                     
                                     # p(strong("Recall this Information about Target Date Funds:")),
                                     # p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Which of these funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g2_q2_b", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g2_q2_c', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 2-C"), value = "Group 2 Question 2-C1",
                                     br(),
                                     p("In the previous question, the implied gross expense ratio had been increased for the fund you had chosen earlier.  As a result, you chose a different fund to invest your retirement funds.  "),
                                     p("What is the lowest gross expense ratio on your originally selected fund where you would not have switched your fund choice?  "),
                                     uiOutput("slider_g2_q2_c1"),
                                     br(),
                                     actionButton('button_c1_to_g2_q3', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 2-C"), value = "Group 2 Question 2-C2",
                                     br(),
                                     p("In the previous question, the implied gross expense ratio had been increased for the fund you had chosen earlier.  You continued to choose the fund that you had originally selected.  "),
                                     p("What is the highest gross expense ratio on your originally selected fund where you would have switched your fund choice?  "),
                                     uiOutput("slider_g2_q2_c2"),
                                     br(),
                                     p(strong("To which fund would you have switched? ")),
                                     br(),
                                     radioButtons("choose_g2_q2_c2", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_c2_to_g2_q3', 'Confirm')
                            )
                          )
                 ),
                 
                 # Group 2 Question 3 ---------------------------------------
                 tabPanel(title = "Question 3", value = "Group 2 Question 3",
                          tabsetPanel(
                            id = "barlist_g2_q3",
                            
                            tabPanel(title = strong("Question 3-A"), value = "Group 2 Question 3-A",
                                     br(),
                                     p("Based on the information you gave in prior questions you anticipate you will retire in the year", textOutput("year_retire_g2_q3_a", inline = TRUE)),
                                     p("You have a retirement plan offered to you by your employer.  Below are a set of Target Date RILAs with target dates that appropriately correspond to your anticipated year of retirement.  The Target Date RILAs shown below all have buffers."),
                                     p("The tables below show information for each investment.",
                                       HTML("<ul><li>The “Implied Gross Expense Ratio” indicates the annual cost for the investment implied by its cap rate.  This number has been estimated to help you determine the implied cost of the product.        
                                             </li><li>The three columns under the “Buffer / Cap Level” heading indicate how the buffer and cap levels for the RILA changes over time. That is, it shows what the buffer and cap will be for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is at your retirement age. 
                                             </li><li>Recall that for RILAs with a buffer:
                                                  <ul><li>The buffer is the amount that limits losses when the return on the associated index is negative.  That is, the buffer is the amount by which the negative return on the index is reduced by to determine the return earned on the RILA.
                                                  </li><li>The cap is the maximum return that can be earned.</li></ul>
                                             </li><li>Recall that equity funds, on average, have a long-term return of 8%.  
                                             </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     ),
                                     p("Your employer has already chosen the following Target Date RILA to invest retirement money for employees that do not actively choose an investment."),
                                     
                                     p(strong("The table may take up to a minute to load")),
                                     br(), 
                                     tableOutput("table_g2_q3_a1"),
                                     br(),
                                     
                                     p("You also have the choice to invest in one of the following investments if you would like:"),
                                     
                                     br(), 
                                     tableOutput("table_g2_q3_a2"),
                                     br(),
                                     
                                     p(strong("Recall the following Information about Target Date RILAs with a Buffer:")),
                                     p("The buffer limits the loss on the RILA when the return on the associated index is negative.  That is, when the index associated with the RILA obtains a negative return, the return on the RILA is reduced by the buffer amount.  For example, suppose a RILA has a buffer -5% and this RILA is associated with the return earned on the S&P 500 index.  If the actual return on the S&P 500 index is -8%, the return on the RILA would only be -3%.  Since the RILA has a buffer which offers protection of -5%, the return on the RILA equals the return on the associated index reduced by this buffer amount."),
                                     p("Please note that if the buffer amount covers more than the negative return on the associated index, the return on the RILA will be zero.  Buffers will not allow for positive returns on the RILA if the associated index had a negative return."),
                                     p("As mentioned earlier RILAs also have a cap, which is the maximum return the RILA can earn.  Suppose the cap on a RILA is 8%.  If the actual return on the index the RILA is associated with is 12%, the RILA would only earn a return of 8% since that is the cap and maximum return the RILA can earn."),
                                     p("Target Date RILAs provide access to equity markets and automatically adjust the level of risk exposure over time. The target date is the approximate date when you expect to begin withdrawing money. As you near your target date,the buffer becomes larger (or the floor becomes less negative), thereby increasing your protection against negative outcomes.  Additionally, the cap rate will be reduced accordingly as there will be less opportunity for large gains. The automatic adjustment in floors (or buffers) and cap rates creates a similar dynamic risk profile to Target Date Funds."),
                                     p(strong("You have a retirement plan offered to you by your employer. Which of the funds below would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g2_q3_a", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g2_q3_b', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 3-B"), value = "Group 2 Question 3-B",
                                     br(),
                                     # p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g2_q1_b", inline = TRUE)),
                                     p("The prices for the plans offered to you by your employer have changed. Below are the new prices."),
                                     # p("The table below shows information for each fund.",
                                     #      HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.     
                                     # </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                     # </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                     # </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     #    ),
                                     p("Your employer has already chosen the following Target Date RILA to invest retirement money for employees that do not actively choose an investment."),
                                     
                                     br(), 
                                     tableOutput("table_g2_q3_b1"),
                                     br(),
                                     
                                     p("You also have the choice to invest in one of the following investments if you would like:"),
                                     
                                     br(), 
                                     tableOutput("table_g2_q3_b2"),
                                     br(),
                                     
                                     # p(strong("Recall this Information about Target Date Funds:")),
                                     # p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Which of these funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g2_q3_b", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g2_q3_c', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 3-C"), value = "Group 2 Question 3-C1",
                                     br(),
                                     p("In the previous question, the implied gross expense ratio had been increased for the fund you had chosen earlier.  As a result, you chose a different fund to invest your retirement funds."),
                                     p("What is the lowest gross expense ratio on your originally selected fund where you would not have switched your fund choice?  "),
                                     uiOutput("slider_g2_q3_c1"),
                                     br(),
                                     actionButton('button_c1_to_g2_q4', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 3-C"), value = "Group 2 Question 3-C2",
                                     br(),
                                     p("In the previous question, the implied gross expense ratio had been increased for the fund you had chosen earlier.  You continued to choose the fund that you had originally selected."),
                                     p("What is the highest gross expense ratio on your originally selected fund where you would have switched your fund choice?  "),
                                     uiOutput("slider_g2_q3_c2"),
                                     br(),
                                     p(strong("To which fund would you have switched?")),
                                     br(),
                                     radioButtons("choose_g2_q3_c2", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_c2_to_g2_q4', 'Confirm')
                            )
                          )
                 ),
                 
                 # Group 2 Question 4 ---------------------------------------
                 tabPanel(title = "Question 4", value = "Group 2 Question 4",
                          tabsetPanel(
                            id = "barlist_g2_q4",
                            
                            tabPanel(title = strong("Question 4-A"), value = "Group 2 Question 4-A",
                                     br(),
                                     p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g2_q4_a", inline = TRUE)),
                                     p("You have a retirement plan offered to you by your employer.  Below are three funds which you can select, all with target dates that appropriately correspond to your anticipated year of retirement.  One fund is a Target Date Fund.  The second fund is a Target Date RILA with a floor and the third fund is a Target Date RILA with a buffer."),
                                     p("The tables below show information for each fund.",
                                       HTML("<ul><li>For the Target Date Fund:
                                                  <ul><li>The Expense Ratio indicates the annual fee that will be charged for managing the fund.  
                                                  </li><li>The three columns under the “Target Allocation” heading for the Target Date Fund indicate how the asset allocation for the fund changes over time.  That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.</li></ul>
                                            </li><li>For the Target Date RILAs:
                                                  <ul><li>The “Implied Gross Expense Ratio” indicates the annual cost for the investment implied by its cap rate.  This number has been estimated to help you determine the implied cost of the product.  
                                                  </li><li>The floor is the guaranteed minimum return that can be earned.
                                                  </li><li>The buffer is the amount that limits losses when the return on the associated index is negative.  That is, the buffer is the amount by which the negative return on the index is reduced by to determine the return earned on the RILA.
                                                  </li><li>The cap is the maximum return that can be earned.  </li></ul>
                                            </li><li>Equity funds, on average, have a long-term return of 8%.  Fixed-income funds, on average, have a long-term return of 3%.
                                            </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     ),
                                     p("Your employer has already chosen the following Target Date Fund to invest retirement money for employees that do not actively choose an investment."),
                                     
                                     p(strong("The table may take up to a minute to load")),
                                     br(), 
                                     tableOutput("table_g2_q4_a1"),
                                     br(),
                                     
                                     p("You also have the choice to invest in one of the following Target Date RILAs if you would like:"),
                                     
                                     br(),
                                     tableOutput("table_g2_q4_a2"),
                                     tableOutput("table_g2_q4_a3"),
                                     br(),
                                     
                                     p(strong("Recall this Information about Target Date Funds:")),
                                     p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Recall the following Information about Target Date RILAs:")),
                                     p("Target Date RILAs provide access to equity markets and automatically adjust the level of risk exposure over time. The target date is the approximate date when you expect to begin withdrawing money. As you near your target date,the buffer becomes larger (or the floor becomes less negative), thereby increasing your protection against negative outcomes.  Additionally, the cap rate will be reduced accordingly as there will be less opportunity for large gains. The automatic adjustment in floors (or buffers) and cap rates creates a similar dynamic risk profile to Target Date Funds."),
                                     p(HTML("<ul><li><b>Floor</b>: The floor offers a minimum guarantee on the return.  The floor is the lowest return the RILA could earn.  For example, suppose a RILA has a floor of -5% and this RILA is associated with the return earned on the S&P 500 index.  If the actual return on the S&P 500 index is -10%, the return on the RILA would only be -5% since that is the lowest return the RILA can earn (or the maximum loss that could be obtained).")),
                                     p(HTML("</li><li><b>Buffer</b>: The buffer limits the loss on the RILA when the return on the associated index is negative.  That is, when the index associated with the RILA obtains a negative return, the return on the RILA is reduced by the buffer amount.  For example, suppose a RILA has a buffer -5% and this RILA is associated with the return earned on the S&P 500 index.  If the actual return on the S&P 500 index is -8%, the return on the RILA would only be -3%.  Since the RILA has a buffer which offers protection of -5%, the return on the RILA equals the return on the associated index reduced by this buffer amount.  ")),
                                     p(HTML("</li><li><b>Cap</b>: The cap is the maximum return the RILA can earn.  Suppose the cap on a RILA is 8%.  If the actual return on the index the RILA is associated with is 12%, the RILA would only earn a return of 8% since that is the cap and maximum return the RILA can earn.</li></ul>")),
                                     p(strong("You have a retirement plan offered to you by your employer. Which of the funds below would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g2_q4_a", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g2_q4_b', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 4-B"), value = "Group 2 Question 4-B",
                                     br(),
                                     # p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g2_q1_b", inline = TRUE)),
                                     p("The prices for the plans offered to you by your employer have changed. Below are the new prices."),
                                     # p("The table below shows information for each fund.",
                                     #      HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.     
                                     # </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                     # </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                     # </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     #    ),
                                     p("Your employer has already chosen the following Target Date Fund to invest retirement money for employees that do not actively choose an investment."),
                                     
                                     br(), 
                                     tableOutput("table_g2_q4_b1"),
                                     br(),
                                     
                                     p("You also have the choice to invest in one of the following Target Date RILAs if you would like:"),
                                     
                                     br(),
                                     tableOutput("table_g2_q4_b2"),
                                     tableOutput("table_g2_q4_b3"),
                                     br(),
                                     
                                     # p(strong("Recall this Information about Target Date Funds:")),
                                     # p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Which of these funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g2_q4_b", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g2_q4_c', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 4-C"), value = "Group 2 Question 4-C1",
                                     br(),
                                     p("In the previous question, the gross expense ratio or implied gross ratio had been increased for the fund you had chosen earlier.  As a result, you chose a different fund to invest your retirement funds."),
                                     p("What is the lowest gross expense ratio on your originally selected fund where you would not have switched your fund choice?  "),
                                     uiOutput("slider_g2_q4_c1"),
                                     br(),
                                     actionButton('button_g2_c1_to_background_info', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 4-C"), value = "Group 2 Question 4-C2",
                                     br(),
                                     p("In the previous question, the gross expense ratio or implied gross expense ratio had been increased for the fund you had chosen earlier.  You continued to choose the fund that you had originally selected."),
                                     p("What is the highest gross expense ratio on your originally selected fund where you would have switched your fund choice?  "),
                                     uiOutput("slider_g2_q4_c2"),
                                     br(),
                                     p(strong("To which fund would you have switched?")),
                                     br(),
                                     radioButtons("choose_g2_q4_c2", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_g2_c2_to_background_info', 'Confirm')
                            )
                          )
                 )# end of tabpanel
                 
      )# end of navbarMenu
      
    } else if (group_assigned == 3) {
      
      # Group 3 ---------------------------------------
      navbarMenu(title = "Survey Questions",
                 
                 # Group 3 Question 1 ---------------------------------------
                 tabPanel(title = "Question 1", value = "Group 3 Question 1",
                          tabsetPanel(
                            id = "barlist_g3_q1",
                            
                            tabPanel(title = strong("Question 1-A"), value = "Group 3 Question 1-A",
                                     br(),
                                     p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g3_q1_a", inline = TRUE)),
                                     p("You have a retirement plan offered to you by your employer.  Below are a set of target date funds with target dates that appropriately correspond to your anticipated year of retirement."),
                                     p("The table below shows information for each fund.  ",
                                       HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.   
                                             </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time.  That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age. 
                                             </li><li>The columns under the heading “Anticipated Retirement Benefit (Monthly Income)” indicate the anticipated <b>monthly</b> income you will receive in retirement if you invest in that target date fund for all your working years.
                                                  <ul><li>Worst Probable Outcome = there is a 1% chance the anticipated monthly benefit is this low
                                                  </li><li>Expected Outcome = the average or expected monthly benefit
                                                  </li><li>Best Probable Outcome = there is a 1% chance the anticipated monthly benefit is this high          
                                                  </li><li>Standard Deviation = the level of variability in the average outcome </li></ul>
                                            </li><li>Recall that equity funds, on average, have a long-term return of 8%.  Fixed-income funds, on average, have a long-term return of 3%.
                                            </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     ),
                                     
                                     p(strong("The table may take up to a minute to load")),
                                     br(), 
                                     tableOutput("table_g3_q1_a"),
                                     p(em("*Note:  The numbers for the anticipated retirement benefit are estimates.  These numbers assume you will be in retirement for 20 years, your wages will grow 3% annually while working, inflation will be 2% annually, and you will contribute 10% of your annual income to the account each year.  The estimates include how much you have already accumulated in your retirement account, as answered earlier in the survey. ")),
                                     br(),
                                     
                                     p(strong("Recall this Information about Target Date Funds:")),
                                     p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("You have a retirement plan offered to you by your employer. Which of the funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g3_q1_a", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g3_q1_b', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 1-B"), value = "Group 3 Question 1-B",
                                     br(),
                                     # p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g4_q1_b", inline = TRUE)),
                                     p("The prices for the plans offered to you by your employer have changed. Below are the new prices."),
                                     # p("The table below shows information for each fund.",
                                     #      HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.     
                                     # </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                     # </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                     # </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     #    ),
                                     br(), 
                                     tableOutput("table_g3_q1_b"),
                                     p(em("*Note:  The numbers for the anticipated retirement benefit are estimates.  These numbers assume you will be in retirement for 20 years, your wages will grow 3% annually while working, inflation will be 2% annually, and you will contribute 10% of your annual income to the account each year.  The estimates include how much you have already accumulated in your retirement account, as answered earlier in the survey. ")),
                                     br(),
                                     
                                     # p(strong("Recall this Information about Target Date Funds:")),
                                     # p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Which of these funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g3_q1_b", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g3_q1_c', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 1-C"), value = "Group 3 Question 1-C1",
                                     br(),
                                     p("In the previous question, the gross expense ratio had been increased for the fund you had chosen earlier.  As a result, you chose a different fund to invest your retirement funds."),
                                     p("What is the lowest gross expense ratio on your originally selected fund where you would not have switched your fund choice?  "),
                                     uiOutput("slider_g3_q1_c1"),
                                     br(),
                                     actionButton('button_c1_to_g3_q2', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 1-C"), value = "Group 3 Question 1-C2",
                                     br(),
                                     p("In the previous question, the gross expense ratio had been increased for the fund you had chosen earlier.  You continued to choose the fund that you had originally selected."),
                                     p("What is the highest gross expense ratio on your originally selected fund where you would have switched your fund choice?  "),
                                     uiOutput("slider_g3_q1_c2"),
                                     br(),
                                     p(strong("To which fund would you have switched?")),
                                     br(),
                                     radioButtons("choose_g3_q1_c2", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_c2_to_g3_q2', 'Confirm')
                            )
                          )
                 ),
                 
                 # Group 3 Question 2 ---------------------------------------
                 tabPanel(title = "Question 2", value = "Group 3 Question 2",
                          tabsetPanel(
                            id = "barlist_g3_q2",
                            
                            tabPanel(title = strong("Question 2-A"), value = "Group 3 Question 2-A",
                                     br(),
                                     p("Based on the information you gave in prior questions you anticipate you will retire in the year", textOutput("year_retire_g3_q2_a", inline = TRUE)),
                                     p("You have a retirement plan offered to you by your employer.  Below are a set of Target Date RILAs with target dates that appropriately correspond to your anticipated year of retirement.  The Target Date RILAs shown below all have floors."),
                                     p("The table below shows information for each investment.  ",
                                       HTML("<ul><li>The “Implied Gross Expense Ratio” indicates the annual cost for the investment implied by its cap rate.  This number has been estimated to help you determine the implied cost of the product.      
                                            </li><li>The three columns under the “Floor / Cap Level” heading indicate how the floor and cap levels for the RILA changes over time. That is, it shows what the floor and cap will be for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is at your retirement age. 
                                            </li><li>Recall that for RILAs with a floor:
                                                  <ul><li>The floor is the guaranteed minimum return that can be earned.
                                                  </li><li>The cap is the maximum return that can be earned.  </li></ul>
                                            </li><li>The columns under the heading “Anticipated Retirement Benefit (Monthly Income)” indicate the anticipated <b>monthly</b> income you will receive in retirement if you invest in that target date fund for all your working years.
                                                  <ul><li>Worst Probable Outcome = there is a 1% chance the anticipated monthly benefit is this low
                                                  </li><li>Expected Outcome = the average or expected monthly benefit
                                                  </li><li>Best Probable Outcome = there is a 1% chance the anticipated monthly benefit is this high
                                                  </li><li>Standard Deviation = the level of variability in the average outcome </li></ul>
                                            </li><li>Recall that equity funds, on average, have a long-term return of 8%.      
                                            </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     ),
                                     
                                     p(strong("The table may take up to a minute to load")),
                                     br(), 
                                     tableOutput("table_g3_q2_a"),
                                     p(em("*Note:  The numbers for the anticipated retirement benefit are estimates.  These numbers assume you will be in retirement for 20 years, your wages will grow 3% annually while working, inflation will be 2% annually, and you will contribute 10% of your annual income to the account each year.  The estimates include how much you have already accumulated in your retirement account, as answered earlier in the survey. ")),
                                     br(),
                                     
                                     p(strong("Recall this information about Target Date RILAs with Floors:")),
                                     p("The floor offers a minimum guarantee on the return.  The floor is the lowest return the RILA could earn.  For example, suppose a RILA has a floor of -5% and this RILA is associated with the return earned on the S&P 500 index.  If the actual return on the S&P 500 index is -10%, the return on the RILA would only be -5% since that is the lowest return the RILA can earn (or the maximum loss that could be obtained)."),
                                     p("As mentioned earlier RILAs also have a cap, which is the maximum return the RILA can earn.  Suppose the cap on a RILA is 8%.  If the actual return on the index the RILA is associated with is 12%, the RILA would only earn a return of 8% since that is the cap and maximum return the RILA can earn."),
                                     p("Target Date RILAs provide access to equity markets and automatically adjust the level of risk exposure over time. The target date is the approximate date when you expect to begin withdrawing money. As you near your target date,the buffer becomes larger (or the floor becomes less negative), thereby increasing your protection against negative outcomes.  Additionally, the cap rate will be reduced accordingly as there will be less opportunity for large gains. The automatic adjustment in floors (or buffers) and cap rates creates a similar dynamic risk profile to Target Date Funds."),
                                     p(strong("You have a retirement plan offered to you by your employer.  Which of the funds below would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g3_q2_a", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g3_q2_b', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 2-B"), value = "Group 3 Question 2-B",
                                     br(),
                                     # p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g4_q1_b", inline = TRUE)),
                                     p("The prices for the plans offered to you by your employer have changed. Below are the new prices."),
                                     # p("The table below shows information for each fund.",
                                     #      HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.     
                                     # </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                     # </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                     # </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     #    ),
                                     
                                     br(), 
                                     tableOutput("table_g3_q2_b"),
                                     p(em("*Note:  The numbers for the anticipated retirement benefit are estimates.  These numbers assume you will be in retirement for 20 years, your wages will grow 3% annually while working, inflation will be 2% annually, and you will contribute 10% of your annual income to the account each year.  The estimates include how much you have already accumulated in your retirement account, as answered earlier in the survey. ")),
                                     br(),
                                     
                                     # p(strong("Recall this Information about Target Date Funds:")),
                                     # p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Which of these funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g3_q2_b", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g3_q2_c', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 2-C"), value = "Group 3 Question 2-C1",
                                     br(),
                                     p("In the previous question, the implied gross expense ratio had been increased for the fund you had chosen earlier.  As a result, you chose a different fund to invest your retirement funds.  "),
                                     p("What is the lowest gross expense ratio on your originally selected fund where you would not have switched your fund choice?  "),
                                     uiOutput("slider_g3_q2_c1"),
                                     br(),
                                     actionButton('button_c1_to_g3_q3', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 2-C"), value = "Group 3 Question 2-C2",
                                     br(),
                                     p("In the previous question, the implied gross expense ratio had been increased for the fund you had chosen earlier.  You continued to choose the fund that you had originally selected.  "),
                                     p("What is the highest gross expense ratio on your originally selected fund where you would have switched your fund choice?  "),
                                     uiOutput("slider_g3_q2_c2"),
                                     br(),
                                     p(strong("To which fund would you have switched?")),
                                     br(),
                                     radioButtons("choose_g3_q2_c2", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_c2_to_g3_q3', 'Confirm')
                            )
                          )
                 ),
                 
                 # Group 3 Question 3 ---------------------------------------
                 tabPanel(title = "Question 3", value = "Group 3 Question 3",
                          tabsetPanel(
                            id = "barlist_g3_q3",
                            
                            tabPanel(title = strong("Question 3-A"), value = "Group 3 Question 3-A",
                                     br(),
                                     p("Based on the information you gave in prior questions you anticipate you will retire in the year", textOutput("year_retire_g3_q3_a", inline = TRUE)),
                                     p("You have a retirement plan offered to you by your employer.  Below are a set of Target Date RILAs with target dates that appropriately correspond to your anticipated year of retirement.  The Target Date RILAs shown below all have buffers."),
                                     p("The table below shows information for each investment.  ",
                                       HTML("<ul><li>The “Implied Gross Expense Ratio” indicates the annual cost for the investment implied by its cap rate.  This number has been estimated to help you determine the implied cost of the product.       
                                            </li><li>The three columns under the “Buffer / Cap Level” heading indicate how the buffer and cap levels for the RILA changes over time. That is, it shows what the buffer and cap will be for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is at your retirement age. 
                                            </li><li>Recall that for RILAs with a buffer:
                                                  <ul><li>The buffer is the amount that limits losses when the return on the associated index is negative.  That is, the buffer is the amount by which the negative return on the index is reduced by to determine the return earned on the RILA.
                                                  </li><li>The cap is the maximum return that can be earned.  </li></ul>
                                            </li><li>The columns under the heading “Anticipated Retirement Benefit (Monthly Income)” indicate the anticipated <b>monthly</b> income you will receive in retirement if you invest in that target date fund for all your working years.
                                                  <ul><li>Worst Probable Outcome = there is a 1% chance the anticipated monthly benefit is this low
                                                  </li><li>Expected Outcome = the average or expected monthly benefit
                                                  </li><li>Best Probable Outcome = there is a 1% chance the anticipated monthly benefit is this high
                                                  </li><li>Standard Deviation = the level of variability in the average outcome</li></ul>
                                            </li><li>Recall that equity funds, on average, have a long-term return of 8%.       
                                            </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     ),
                                     
                                     p(strong("The table may take up to a minute to load")),
                                     br(), 
                                     tableOutput("table_g3_q3_a"),
                                     p(em("*Note:  The numbers for the anticipated retirement benefit are estimates.  These numbers assume you will be in retirement for 20 years, your wages will grow 3% annually while working, inflation will be 2% annually, and you will contribute 10% of your annual income to the account each year.  The estimates include how much you have already accumulated in your retirement account, as answered earlier in the survey. ")),
                                     br(),
                                     
                                     p(strong("Recall the following Information about Target Date RILAs with a Buffer:")),
                                     p("The buffer limits the loss on the RILA when the return on the associated index is negative.  That is, when the index associated with the RILA obtains a negative return, the return on the RILA is reduced by the buffer amount.  For example, suppose a RILA has a buffer -5% and this RILA is associated with the return earned on the S&P 500 index.  If the actual return on the S&P 500 index is -8%, the return on the RILA would only be -3%.  Since the RILA has a buffer which offers protection of -5%, the return on the RILA equals the return on the associated index reduced by this buffer amount.  "),
                                     p("Please note that if the buffer amount covers more than the negative return on the associated index, the return on the RILA will be zero.  Buffers will not allow for positive returns on the RILA if the associated index had a negative return."),
                                     p("As mentioned earlier RILAs also have a cap, which is the maximum return the RILA can earn.  Suppose the cap on a RILA is 8%.  If the actual return on the index the RILA is associated with is 12%, the RILA would only earn a return of 8% since that is the cap and maximum return the RILA can earn."),
                                     p("Target Date RILAs provide access to equity markets and automatically adjust the level of risk exposure over time. The target date is the approximate date when you expect to begin withdrawing money. As you near your target date,the buffer becomes larger (or the floor becomes less negative), thereby increasing your protection against negative outcomes.  Additionally, the cap rate will be reduced accordingly as there will be less opportunity for large gains. The automatic adjustment in floors (or buffers) and cap rates creates a similar dynamic risk profile to Target Date Funds."),
                                     p(strong("You have a retirement plan offered to you by your employer.  Which of the funds below would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g3_q3_a", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g3_q3_b', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 3-B"), value = "Group 3 Question 3-B",
                                     br(),
                                     # p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g4_q1_b", inline = TRUE)),
                                     p("The prices for the plans offered to you by your employer have changed. Below are the new prices."),
                                     # p("The table below shows information for each fund.",
                                     #      HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.     
                                     # </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                     # </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                     # </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     #    ),
                                     
                                     br(), 
                                     tableOutput("table_g3_q3_b"),
                                     p(em("*Note:  The numbers for the anticipated retirement benefit are estimates.  These numbers assume you will be in retirement for 20 years, your wages will grow 3% annually while working, inflation will be 2% annually, and you will contribute 10% of your annual income to the account each year.  The estimates include how much you have already accumulated in your retirement account, as answered earlier in the survey. ")),
                                     br(),
                                     
                                     # p(strong("Recall this Information about Target Date Funds:")),
                                     # p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Which of these funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g3_q3_b", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g3_q3_c', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 3-C"), value = "Group 3 Question 3-C1",
                                     br(),
                                     p("In the previous question, the implied gross expense ratio had been increased for the fund you had chosen earlier.  As a result, you chose a different fund to invest your retirement funds.  "),
                                     p("What is the lowest gross expense ratio on your originally selected fund where you would not have switched your fund choice?  "),
                                     uiOutput("slider_g3_q3_c1"),
                                     br(),
                                     actionButton('button_c1_to_g3_q4', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 3-C"), value = "Group 3 Question 3-C2",
                                     br(),
                                     p("In the previous question, the implied gross expense ratio had been increased for the fund you had chosen earlier.  You continued to choose the fund that you had originally selected.  "),
                                     p("What is the highest gross expense ratio on your originally selected fund where you would have switched your fund choice?  "),
                                     uiOutput("slider_g3_q3_c2"),
                                     br(),
                                     p(strong("To which fund would you have switched?  ")),
                                     br(),
                                     radioButtons("choose_g3_q3_c2", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_c2_to_g3_q4', 'Confirm')
                            )
                          )
                 ),
                 
                 # Group 3 Question 4 ---------------------------------------
                 tabPanel(title = "Question 4", value = "Group 3 Question 4",
                          tabsetPanel(
                            id = "barlist_g3_q4",
                            
                            tabPanel(title = strong("Question 4-A"), value = "Group 3 Question 4-A",
                                     br(),
                                     p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g3_q4_a", inline = TRUE)),
                                     p("You have a retirement plan offered to you by your employer.  Below are three funds which you can select, all with target dates that appropriately correspond to your anticipated year of retirement.  One fund is a Target Date Fund.  The second fund is a Target Date RILA with a floor and the third fund is a Target Date RILA with a buffer."),
                                     p("The tables below show information for each fund.",
                                       HTML("<ul><li>For the Target Date Fund:
                                                  <ul><li>The Expense Ratio indicates the annual fee that will be charged for managing the fund.  
                                                  </li><li>The three columns under the “Target Allocation” heading for the Target Date Fund indicate how the asset allocation for the fund changes over time.  That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age. </li></ul>
                                            </li><li>For the Target Date RILAs:
                                                  <ul><li>The “Implied Gross Expense Ratio” indicates the annual cost for the investment implied by its cap rate.  This number has been estimated to help you determine the implied cost of the product.  
                                                  </li><li>The floor is the guaranteed minimum return that can be earned.
                                                  </li><li>The buffer is the amount that limits losses when the return on the associated index is negative.  That is, the buffer is the amount by which the negative return on the index is reduced by to determine the return earned on the RILA.
                                                  </li><li>The cap is the maximum return that can be earned.  </li></ul>
                                            </li><li>The columns under the heading “Anticipated Retirement Benefit (Monthly Income)” indicate the anticipated <b>monthly</b> income you will receive in retirement if you invest in that target date fund for all your working years.
                                                  <ul><li>Worst Probable Outcome = there is a 1% chance the anticipated monthly benefit is this low
                                                  </li><li>Expected Outcome = the average or expected monthly benefit
                                                  </li><li>Best Probable Outcome = there is a 1% chance the anticipated monthly benefit is this high
                                                  </li><li>Standard Deviation = the level of variability in the average outcome</li></ul>
                                            </li><li>Equity funds, on average, have a long-term return of 8%.  Fixed-income funds, on average, have a long-term return of 3%.
                                            </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     ),
                                     
                                     p(strong("The table may take up to a minute to load")),
                                     br(), 
                                     tableOutput("table_g3_q4_a1"),
                                     tableOutput("table_g3_q4_a2"),
                                     tableOutput("table_g3_q4_a3"),
                                     p(em("*Note:  The numbers for the anticipated retirement benefit are estimates.  These numbers assume you will be in retirement for 20 years, your wages will grow 3% annually while working, inflation will be 2% annually, and you will contribute 10% of your annual income to the account each year.  The estimates include how much you have already accumulated in your retirement account, as answered earlier in the survey. ")),
                                     br(),
                                     
                                     p(strong("Recall this Information about Target Date Funds:")),
                                     p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Recall the following Information about Target Date RILAs:")),
                                     p("Target Date RILAs provide access to equity markets and automatically adjust the level of risk exposure over time. The target date is the approximate date when you expect to begin withdrawing money. As you near your target date,the buffer becomes larger (or the floor becomes less negative), thereby increasing your protection against negative outcomes.  Additionally, the cap rate will be reduced accordingly as there will be less opportunity for large gains. The automatic adjustment in floors (or buffers) and cap rates creates a similar dynamic risk profile to Target Date Funds."),
                                     p(HTML("<ul><li><b>Floor</b>: The floor offers a minimum guarantee on the return.  The floor is the lowest return the RILA could earn.  For example, suppose a RILA has a floor of -5% and this RILA is associated with the return earned on the S&P 500 index.  If the actual return on the S&P 500 index is -10%, the return on the RILA would only be -5% since that is the lowest return the RILA can earn (or the maximum loss that could be obtained).")),
                                     p(HTML("</li><li><b>Buffer</b>: The buffer limits the loss on the RILA when the return on the associated index is negative.  That is, when the index associated with the RILA obtains a negative return, the return on the RILA is reduced by the buffer amount.  For example, suppose a RILA has a buffer -5% and this RILA is associated with the return earned on the S&P 500 index.  If the actual return on the S&P 500 index is -8%, the return on the RILA would only be -3%.  Since the RILA has a buffer which offers protection of -5%, the return on the RILA equals the return on the associated index reduced by this buffer amount.  ")),
                                     p(HTML("</li><li><b>Cap</b>: The cap is the maximum return the RILA can earn.  Suppose the cap on a RILA is 8%.  If the actual return on the index the RILA is associated with is 12%, the RILA would only earn a return of 8% since that is the cap and maximum return the RILA can earn.</li></ul>")),
                                     p(strong("You have a retirement plan offered to you by your employer.  Which of the funds below would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g3_q4_a", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g3_q4_b', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 4-B"), value = "Group 3 Question 4-B",
                                     br(),
                                     # p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g4_q1_b", inline = TRUE)),
                                     p("The prices for the plans offered to you by your employer have changed. Below are the new prices."),
                                     # p("The table below shows information for each fund.",
                                     #      HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.     
                                     # </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                     # </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                     # </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     #    ),
                                     
                                     br(), 
                                     tableOutput("table_g3_q4_b1"),
                                     tableOutput("table_g3_q4_b2"),
                                     tableOutput("table_g3_q4_b3"),
                                     p(em("*Note:  The numbers for the anticipated retirement benefit are estimates.  These numbers assume you will be in retirement for 20 years, your wages will grow 3% annually while working, inflation will be 2% annually, and you will contribute 10% of your annual income to the account each year.  The estimates include how much you have already accumulated in your retirement account, as answered earlier in the survey. ")),
                                     br(),
                                     
                                     # p(strong("Recall this Information about Target Date Funds:")),
                                     # p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Which of these funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g3_q4_b", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g3_q4_c', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 4-C"), value = "Group 3 Question 4-C1",
                                     br(),
                                     p("In the previous question, the gross expense ratio or implied gross ratio had been increased for the fund you had chosen earlier.  As a result, you chose a different fund to invest your retirement funds."),
                                     p("What is the lowest gross expense ratio on your originally selected fund where you would not have switched your fund choice?  "),
                                     uiOutput("slider_g3_q4_c1"),
                                     br(),
                                     actionButton('button_g3_c1_to_background_info', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 4-C"), value = "Group 3 Question 4-C2",
                                     br(),
                                     p("In the previous question, the gross expense ratio or implied gross expense ratio had been increased for the fund you had chosen earlier.  You continued to choose the fund that you had originally selected.  "),
                                     p("What is the highest gross expense ratio on your originally selected fund where you would have switched your fund choice?  "),
                                     uiOutput("slider_g3_q4_c2"),
                                     br(),
                                     p(strong("To which fund would you have switched?")),
                                     br(),
                                     radioButtons("choose_g3_q4_c2", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_g3_c2_to_background_info', 'Confirm')
                            )
                          )
                 )# end of tabpanel
                 
      )# end of navbarMenu
      
    } else {
      
      # Group 4 ---------------------------------------
      navbarMenu(title = "Survey Questions",
                 
                 # Group 4 Question 1 ---------------------------------------
                 tabPanel(title = "Question 1", value = "Group 4 Question 1",
                          tabsetPanel(
                            id = "barlist_g4_q1",
                            
                            tabPanel(title = strong("Question 1-A"), value = "Group 4 Question 1-A",
                                     br(),
                                     p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g4_q1_a", inline = TRUE)),
                                     p("You have a retirement plan offered to you by your employer.  Below are a set of target date funds with target dates that appropriately correspond to your anticipated year of retirement."),
                                     p("The table below shows information for each fund.",
                                       HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.  
                                             </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time.  That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age. 
                                             </li><li>The columns under the heading “Anticipated Retirement Benefit (Monthly Income)” indicate the anticipated <b>monthly</b> income you will receive in retirement if you invest in that target date fund for all your working years.
                                                  <ul><li>Worst Probable Outcome = there is a 1% chance the anticipated monthly benefit is this low
                                                  </li><li>Expected Outcome = the average or expected monthly benefit
                                                  </li><li>Best Probable Outcome = there is a 1% chance the anticipated monthly benefit is this high         
                                                  </li><li>Standard Deviation = the level of variability in the average outcome</li></ul>
                                            </li><li>Recall that equity funds, on average, have a long-term return of 8%.  Fixed-income funds, on average, have a long-term return of 3%.
                                            </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     ),
                                     p("Your employer has already chosen the following target date fund to invest retirement money for employees that do not actively choose an investment."),
                                     
                                     p(strong("The table may take up to a minute to load")),
                                     br(), 
                                     tableOutput("table_g4_q1_a1"),
                                     br(),
                                     
                                     p("You also have the choice to invest in one of the following funds if you would like: "),
                                     
                                     br(), 
                                     tableOutput("table_g4_q1_a2"),
                                     p(em("*Note:  The numbers for the anticipated retirement benefit are estimates.  These numbers assume you will be in retirement for 20 years, your wages will grow 3% annually while working, inflation will be 2% annually, and you will contribute 10% of your annual income to the account each year.  The estimates include how much you have already accumulated in your retirement account, as answered earlier in the survey. ")),
                                     br(),
                                     
                                     p(strong("Recall this Information about Target Date Funds:")),
                                     p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("You have a retirement plan offered to you by your employer.  Which of the funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g4_q1_a", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g4_q1_b', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 1-B"), value = "Group 4 Question 1-B",
                                     br(),
                                     # p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g4_q1_b", inline = TRUE)),
                                     p("The prices for the plans offered to you by your employer have changed. Below are the new prices."),
                                     # p("The table below shows information for each fund.",
                                     #      HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.     
                                     # </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                     # </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                     # </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     #    ),
                                     p("Your employer has already chosen the following target date fund to invest retirement money for employees that do not actively choose an investment."),
                                     
                                     br(), 
                                     tableOutput("table_g4_q1_b1"),
                                     br(),
                                     
                                     p("You also have the choice to invest in one of the following funds if you would like: "),
                                     
                                     br(), 
                                     tableOutput("table_g4_q1_b2"),
                                     p(em("*Note:  The numbers for the anticipated retirement benefit are estimates.  These numbers assume you will be in retirement for 20 years, your wages will grow 3% annually while working, inflation will be 2% annually, and you will contribute 10% of your annual income to the account each year.  The estimates include how much you have already accumulated in your retirement account, as answered earlier in the survey. ")),
                                     br(),
                                     
                                     # p(strong("Recall this Information about Target Date Funds:")),
                                     # p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Which of these funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g4_q1_b", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g4_q1_c', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 1-C"), value = "Group 4 Question 1-C1",
                                     br(),
                                     p("In the previous question, the gross expense ratio had been increased for the fund you had chosen earlier.  As a result, you chose a different fund to invest your retirement funds."),
                                     p("What is the lowest gross expense ratio on your originally selected fund where you would not have switched your fund choice?  "),
                                     uiOutput("slider_g4_q1_c1"),
                                     br(),
                                     actionButton('button_c1_to_g4_q2', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 1-C"), value = "Group 4 Question 1-C2",
                                     br(),
                                     p("In the previous question, the gross expense ratio had been increased for the fund you had chosen earlier.  You continued to choose the fund that you had originally selected.  "),
                                     p("What is the highest gross expense ratio on your originally selected fund where you would have switched your fund choice?  "),
                                     uiOutput("slider_g4_q1_c2"),
                                     br(),
                                     p(strong("To which fund would you have switched? ")),
                                     br(),
                                     radioButtons("choose_g4_q1_c2", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_c2_to_g4_q2', 'Confirm')
                            )
                          )
                 ),
                 
                 # Group 4 Question 2 ---------------------------------------
                 tabPanel(title = "Question 2", value = "Group 4 Question 2",
                          tabsetPanel(
                            id = "barlist_g4_q2",
                            
                            tabPanel(title = strong("Question 2-A"), value = "Group 4 Question 2-A",
                                     br(),
                                     p("Based on the information you gave in prior questions you anticipate you will retire in the year", textOutput("year_retire_g4_q2_a", inline = TRUE)),
                                     p("You have a retirement plan offered to you by your employer.  Below are a set of Target Date RILAs with target dates that appropriately correspond to your anticipated year of retirement.  The Target Date RILAs shown below all have floors."),
                                     p("The table below shows information for each investment.",
                                       HTML("<ul><li>The “Implied Gross Expense Ratio” indicates the annual cost for the investment implied by its cap rate.  This number has been estimated to help you determine the implied cost of the product.  
                                            </li><li>The three columns under the “Floor / Cap Level” heading indicate how the floor and cap levels for the RILA changes over time. That is, it shows what the floor and cap will be for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is at your retirement age. 
                                            </li><li>Recall that for RILAs with a floor:
                                                  <ul><li>The floor is the guaranteed minimum return that can be earned.
                                                  </li><li>The cap is the maximum return that can be earned. </li></ul>
                                            </li><li>The columns under the heading “Anticipated Retirement Benefit (Monthly Income)” indicate the anticipated <b>monthly</b> income you will receive in retirement if you invest in that target date fund for all your working years.
                                                  <ul><li>Worst Probable Outcome = there is a 1% chance the anticipated monthly benefit is this low
                                                  </li><li>Expected Outcome = the average or expected monthly benefit
                                                  </li><li>Best Probable Outcome = there is a 1% chance the anticipated monthly benefit is this high
                                                  </li><li>Standard Deviation = the level of variability in the average outcome </li></ul>
                                            </li><li>Recall that equity funds, on average, have a long-term return of 8%.    
                                            </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     ),
                                     p("Your employer has already chosen the following Target Date RILA to invest retirement money for employees that do not actively choose an investment."),
                                     
                                     p(strong("The table may take up to a minute to load")),
                                     br(), 
                                     tableOutput("table_g4_q2_a1"),
                                     br(),
                                     
                                     p("You also have the choice to invest in one of the following investments if you would like: "),
                                     
                                     br(),
                                     tableOutput("table_g4_q2_a2"),
                                     p(em("*Note:  The numbers for the anticipated retirement benefit are estimates.  These numbers assume you will be in retirement for 20 years, your wages will grow 3% annually while working, inflation will be 2% annually, and you will contribute 10% of your annual income to the account each year.  The estimates include how much you have already accumulated in your retirement account, as answered earlier in the survey.  ")),
                                     br(),
                                     
                                     p(strong("Recall this information about Target Date RILAs with Floors:")),
                                     p("The floor offers a minimum guarantee on the return.  The floor is the lowest return the RILA could earn.  For example, suppose a RILA has a floor of -5% and this RILA is associated with the return earned on the S&P 500 index.  If the actual return on the S&P 500 index is -10%, the return on the RILA would only be -5% since that is the lowest return the RILA can earn (or the maximum loss that could be obtained)."),
                                     p("As mentioned earlier RILAs also have a cap, which is the maximum return the RILA can earn.  Suppose the cap on a RILA is 8%.  If the actual return on the index the RILA is associated with is 12%, the RILA would only earn a return of 8% since that is the cap and maximum return the RILA can earn."),
                                     p("Target Date RILAs provide access to equity markets and automatically adjust the level of risk exposure over time. The target date is the approximate date when you expect to begin withdrawing money. As you near your target date,the buffer becomes larger (or the floor becomes less negative), thereby increasing your protection against negative outcomes.  Additionally, the cap rate will be reduced accordingly as there will be less opportunity for large gains. The automatic adjustment in floors (or buffers) and cap rates creates a similar dynamic risk profile to Target Date Funds."),
                                     p(strong("You have a retirement plan offered to you by your employer.  Which of the funds below would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g4_q2_a", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g4_q2_b', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 2-B"), value = "Group 4 Question 2-B",
                                     br(),
                                     # p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g4_q1_b", inline = TRUE)),
                                     p("The prices for the plans offered to you by your employer have changed. Below are the new prices."),
                                     # p("The table below shows information for each fund.",
                                     #      HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.     
                                     # </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                     # </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                     # </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     #    ),
                                     p("Your employer has already chosen the following Target Date RILA to invest retirement money for employees that do not actively choose an investment."),
                                     
                                     br(), 
                                     tableOutput("table_g4_q2_b1"),
                                     br(),
                                     
                                     p("You also have the choice to invest in one of the following investments if you would like: "),
                                     
                                     br(),
                                     tableOutput("table_g4_q2_b2"),
                                     p(em("*Note:  The numbers for the anticipated retirement benefit are estimates.  These numbers assume you will be in retirement for 20 years, your wages will grow 3% annually while working, inflation will be 2% annually, and you will contribute 10% of your annual income to the account each year.  The estimates include how much you have already accumulated in your retirement account, as answered earlier in the survey.")),
                                     br(),
                                     
                                     # p(strong("Recall this Information about Target Date Funds:")),
                                     # p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Which of these funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g4_q2_b", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g4_q2_c', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 2-C"), value = "Group 4 Question 2-C1",
                                     br(),
                                     p("In the previous question, the implied gross expense ratio had been increased for the fund you had chosen earlier.  As a result, you chose a different fund to invest your retirement funds.  "),
                                     p("What is the lowest gross expense ratio on your originally selected fund where you would not have switched your fund choice?  "),
                                     uiOutput("slider_g4_q2_c1"),
                                     br(),
                                     actionButton('button_c1_to_g4_q3', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 2-C"), value = "Group 4 Question 2-C2",
                                     br(),
                                     p("In the previous question, the implied gross expense ratio had been increased for the fund you had chosen earlier.  You continued to choose the fund that you had originally selected.  "),
                                     p("What is the highest gross expense ratio on your originally selected fund where you would have switched your fund choice?  "),
                                     uiOutput("slider_g4_q2_c2"),
                                     br(),
                                     p(strong("To which fund would you have switched? ")),
                                     br(),
                                     radioButtons("choose_g4_q2_c2", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_c2_to_g4_q3', 'Confirm')
                            )
                          )
                 ),
                 
                 # Group 4 Question 3 ---------------------------------------
                 tabPanel(title = "Question 3", value = "Group 4 Question 3",
                          tabsetPanel(
                            id = "barlist_g4_q3",
                            
                            tabPanel(title = strong("Question 3-A"), value = "Group 4 Question 3-A",
                                     br(),
                                     p("Based on the information you gave in prior questions you anticipate you will retire in the year", textOutput("year_retire_g4_q3_a", inline = TRUE)),
                                     p("You have a retirement plan offered to you by your employer.  Below are a set of Target Date RILAs with target dates that appropriately correspond to your anticipated year of retirement.  The Target Date RILAs shown below all have buffers."),
                                     p("The table below shows information for each investment.",
                                       HTML("<ul><li>The “Implied Gross Expense Ratio” indicates the annual cost for the investment implied by its cap rate.  This number has been estimated to help you determine the implied cost of the product.
                                            </li><li>The three columns under the “Buffer / Cap Level” heading indicate how the buffer and cap levels for the RILA changes over time. That is, it shows what the buffer and cap will be for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is at your retirement age. 
                                            </li><li>Recall that for RILAs with a buffer:
                                                  <ul><li>The buffer is the amount that limits losses when the return on the associated index is negative.  That is, the buffer is the amount by which the negative return on the index is reduced by to determine the return earned on the RILA.
                                                  </li><li>The cap is the maximum return that can be earned.</li></ul>
                                            </li><li>The columns under the heading “Anticipated Retirement Benefit (Monthly Income)” indicate the anticipated <b>monthly</b> income you will receive in retirement if you invest in that target date fund for all your working years.
                                                  <ul><li>Worst Probable Outcome = there is a 1% chance the anticipated monthly benefit is this low
                                                  </li><li>Expected Outcome = the average or expected monthly benefit
                                                  </li><li>Best Probable Outcome = there is a 1% chance the anticipated monthly benefit is this high
                                                  </li><li>Standard Deviation = the level of variability in the average outcome</li></ul>
                                            </li><li>Recall that equity funds, on average, have a long-term return of 8%.   
                                            </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     ),
                                     p("Your employer has already chosen the following Target Date RILA to invest retirement money for employees that do not actively choose an investment."),
                                     
                                     p(strong("The table may take up to a minute to load")),
                                     br(), 
                                     tableOutput("table_g4_q3_a1"),
                                     br(),
                                     
                                     p("You also have the choice to invest in one of the following investments if you would like: "),
                                     
                                     br(),
                                     tableOutput("table_g4_q3_a2"),
                                     p(em("*Note:  The numbers for the anticipated retirement benefit are estimates.  These numbers assume you will be in retirement for 20 years, your wages will grow 3% annually while working, inflation will be 2% annually, and you will contribute 10% of your annual income to the account each year.  The estimates include how much you have already accumulated in your retirement account, as answered earlier in the survey. ")),
                                     br(),
                                     
                                     p(strong("Recall the following Information about Target Date RILAs with a Buffer:")),
                                     p("The buffer limits the loss on the RILA when the return on the associated index is negative.  That is, when the index associated with the RILA obtains a negative return, the return on the RILA is reduced by the buffer amount.  For example, suppose a RILA has a buffer -5% and this RILA is associated with the return earned on the S&P 500 index.  If the actual return on the S&P 500 index is -8%, the return on the RILA would only be -3%.  Since the RILA has a buffer which offers protection of -5%, the return on the RILA equals the return on the associated index reduced by this buffer amount."),
                                     p("Please note that if the buffer amount covers more than the negative return on the associated index, the return on the RILA will be zero.  Buffers will not allow for positive returns on the RILA if the associated index had a negative return."),
                                     p("As mentioned earlier RILAs also have a cap, which is the maximum return the RILA can earn.  Suppose the cap on a RILA is 8%.  If the actual return on the index the RILA is associated with is 12%, the RILA would only earn a return of 8% since that is the cap and maximum return the RILA can earn."),
                                     p("Target Date RILAs provide access to equity markets and automatically adjust the level of risk exposure over time. The target date is the approximate date when you expect to begin withdrawing money. As you near your target date,the buffer becomes larger (or the floor becomes less negative), thereby increasing your protection against negative outcomes.  Additionally, the cap rate will be reduced accordingly as there will be less opportunity for large gains. The automatic adjustment in floors (or buffers) and cap rates creates a similar dynamic risk profile to Target Date Funds."),
                                     p(strong("You have a retirement plan offered to you by your employer.  Which of the funds below would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g4_q3_a", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g4_q3_b', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 3-B"), value = "Group 4 Question 3-B",
                                     br(),
                                     # p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g4_q1_b", inline = TRUE)),
                                     p("The prices for the plans offered to you by your employer have changed. Below are the new prices."),
                                     # p("The table below shows information for each fund.",
                                     #      HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.     
                                     # </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                     # </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                     # </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     #    ),
                                     p("Your employer has already chosen the following Target Date RILA to invest retirement money for employees that do not actively choose an investment."),
                                     
                                     br(), 
                                     tableOutput("table_g4_q3_b1"),
                                     br(),
                                     
                                     p("You also have the choice to invest in one of the following investments if you would like:"),
                                     
                                     br(),
                                     tableOutput("table_g4_q3_b2"),
                                     p(em("*Note:  The numbers for the anticipated retirement benefit are estimates.  These numbers assume you will be in retirement for 20 years, your wages will grow 3% annually while working, inflation will be 2% annually, and you will contribute 10% of your annual income to the account each year.  The estimates include how much you have already accumulated in your retirement account, as answered earlier in the survey. ")),
                                     br(),
                                     
                                     # p(strong("Recall this Information about Target Date Funds:")),
                                     # p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Which of these funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g4_q3_b", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g4_q3_c', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 3-C"), value = "Group 4 Question 3-C1",
                                     br(),
                                     p("In the previous question, the implied gross expense ratio had been increased for the fund you had chosen earlier.  As a result, you chose a different fund to invest your retirement funds."),
                                     p("What is the lowest gross expense ratio on your originally selected fund where you would not have switched your fund choice?  "),
                                     uiOutput("slider_g4_q3_c1"),
                                     br(),
                                     actionButton('button_c1_to_g4_q4', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 3-C"), value = "Group 4 Question 3-C2",
                                     br(),
                                     p("In the previous question, the implied gross expense ratio had been increased for the fund you had chosen earlier.  You continued to choose the fund that you had originally selected."),
                                     p("What is the highest gross expense ratio on your originally selected fund where you would have switched your fund choice?  "),
                                     uiOutput("slider_g4_q3_c2"),
                                     br(),
                                     p(strong("To which fund would you have switched?")),
                                     br(),
                                     radioButtons("choose_g4_q3_c2", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_c2_to_g4_q4', 'Confirm')
                            )
                          )
                 ),
                 
                 # Group 4 Question 4 ---------------------------------------
                 tabPanel(title = "Question 4", value = "Group 4 Question 4",
                          tabsetPanel(
                            id = "barlist_g4_q4",
                            
                            tabPanel(title = strong("Question 4-A"), value = "Group 4 Question 4-A",
                                     br(),
                                     p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g4_q4_a", inline = TRUE)),
                                     p("You have a retirement plan offered to you by your employer.  Below are three funds which you can select, all with target dates that appropriately correspond to your anticipated year of retirement.  One fund is a Target Date Fund.  The second fund is a Target Date RILA with a floor and the third fund is a Target Date RILA with a buffer."),
                                     p("The tables below show information for each fund.",
                                       HTML("<ul><li>For the Target Date Fund:
                                                  <ul><li>The Expense Ratio indicates the annual fee that will be charged for managing the fund.  
                                                  </li><li>The three columns under the “Target Allocation” heading for the Target Date Fund indicate how the asset allocation for the fund changes over time.  That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.  </li></ul>
                                            </li><li>For the Target Date RILAs:
                                                  <ul><li>The “Implied Gross Expense Ratio” indicates the annual cost for the investment implied by its cap rate.  This number has been estimated to help you determine the implied cost of the product.  
                                                  </li><li>The floor is the guaranteed minimum return that can be earned.
                                                  </li><li>The buffer is the amount that limits losses when the return on the associated index is negative.  That is, the buffer is the amount by which the negative return on the index is reduced by to determine the return earned on the RILA.
                                                  </li><li>The cap is the maximum return that can be earned.</li></ul>
                                            </li><li>The columns under the heading “Anticipated Retirement Benefit (Monthly Income)” indicate the anticipated <b>monthly</b> income you will receive in retirement if you invest in that target date fund for all your working years.
                                                  <ul><li>Worst Probable Outcome = there is a 1% chance the anticipated monthly benefit is this low
                                                  </li><li>Expected Outcome = the average or expected monthly benefit
                                                  </li><li>Best Probable Outcome = there is a 1% chance the anticipated monthly benefit is this high
                                                  </li><li>Standard Deviation = the level of variability in the average outcome</li></ul>
                                            </li><li>Equity funds, on average, have a long-term return of 8%.  Fixed-income funds, on average, have a long-term return of 3%.
                                            </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     ),
                                     p("Your employer has already chosen the following target date fund to invest retirement money for employees that do not actively choose an investment."),
                                     
                                     p(strong("The table may take up to a minute to load")),
                                     br(), 
                                     tableOutput("table_g4_q4_a1"),
                                     br(),
                                     
                                     p("You also have the choice to invest in one of the following investments if you would like: "),
                                     
                                     br(),
                                     tableOutput("table_g4_q4_a2"),
                                     tableOutput("table_g4_q4_a3"),
                                     p(em("*Note:  The numbers for the anticipated retirement benefit are estimates.  These numbers assume you will be in retirement for 20 years, your wages will grow 3% annually while working, inflation will be 2% annually, and you will contribute 10% of your annual income to the account each year.  The estimates include how much you have already accumulated in your retirement account, as answered earlier in the survey. ")),
                                     br(),
                                     
                                     p(strong("Recall this Information about Target Date Funds:")),
                                     p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Recall the following Information about Target Date RILAs:")),
                                     p("Target Date RILAs provide access to equity markets and automatically adjust the level of risk exposure over time. The target date is the approximate date when you expect to begin withdrawing money. As you near your target date,the buffer becomes larger (or the floor becomes less negative), thereby increasing your protection against negative outcomes.  Additionally, the cap rate will be reduced accordingly as there will be less opportunity for large gains. The automatic adjustment in floors (or buffers) and cap rates creates a similar dynamic risk profile to Target Date Funds."),
                                     p(HTML("<ul><li><b>Floor</b>: The floor offers a minimum guarantee on the return.  The floor is the lowest return the RILA could earn.  For example, suppose a RILA has a floor of -5% and this RILA is associated with the return earned on the S&P 500 index.  If the actual return on the S&P 500 index is -10%, the return on the RILA would only be -5% since that is the lowest return the RILA can earn (or the maximum loss that could be obtained).")),
                                     p(HTML("</li><li><b>Buffer</b>: The buffer limits the loss on the RILA when the return on the associated index is negative.  That is, when the index associated with the RILA obtains a negative return, the return on the RILA is reduced by the buffer amount.  For example, suppose a RILA has a buffer -5% and this RILA is associated with the return earned on the S&P 500 index.  If the actual return on the S&P 500 index is -8%, the return on the RILA would only be -3%.  Since the RILA has a buffer which offers protection of -5%, the return on the RILA equals the return on the associated index reduced by this buffer amount.  ")),
                                     p(HTML("</li><li><b>Cap</b>: The cap is the maximum return the RILA can earn.  Suppose the cap on a RILA is 8%.  If the actual return on the index the RILA is associated with is 12%, the RILA would only earn a return of 8% since that is the cap and maximum return the RILA can earn.</li></ul>")),
                                     p(strong("You have a retirement plan offered to you by your employer.  Which of the funds below would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g4_q4_a", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g4_q4_b', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 4-B"), value = "Group 4 Question 4-B",
                                     br(),
                                     # p("Based on the information you gave in the prior questions you anticipate you will retire in the year", textOutput("year_retire_g4_q1_b", inline = TRUE)),
                                     p("The prices for the plans offered to you by your employer have changed. Below are the new prices."),
                                     # p("The table below shows information for each fund.",
                                     #      HTML("<ul><li>The “Gross Expense Ratio” indicates the annual fee that will be charged for managing the fund.     
                                     # </li><li>The three columns under the “Target Allocation” heading indicate how the asset allocation for the fund changes over time. That is, it shows what portion of the fund is invested in riskier equity versus safer fixed income securities for three different points in time: current, middle which is mid-way between now and your retirement age, and final which is the anticipated fund allocation at your retirement age.
                                     # </li><li>On average, equity funds tend to have a higher rate of return than fixed-income funds, but equity funds are also more risky.
                                     # </li><li>Recall that funds with greater risk (volatility) typically have higher returns.</li></ul>")
                                     #    ),
                                     p("Your employer has already chosen the following target date fund to invest retirement money for employees that do not actively choose an investment."),
                                     
                                     br(), 
                                     tableOutput("table_g4_q4_b1"),
                                     br(),
                                     
                                     p("You also have the choice to invest in one of the following investments if you would like: "),
                                     
                                     br(),
                                     tableOutput("table_g4_q4_b2"),
                                     tableOutput("table_g4_q4_b3"),
                                     p(em("*Note:  The numbers for the anticipated retirement benefit are estimates.  These numbers assume you will be in retirement for 20 years, your wages will grow 3% annually while working, inflation will be 2% annually, and you will contribute 10% of your annual income to the account each year.  The estimates include how much you have already accumulated in your retirement account, as answered earlier in the survey. ")),
                                     br(),
                                     
                                     # p(strong("Recall this Information about Target Date Funds:")),
                                     # p("Recall Target Date Funds seek high total return over time through a combination of capital appreciation and fixed income. These funds are designed to provide a single diversified portfolio managed with a target retirement date in mind. The target date is the approximate date when investors expect to begin withdrawing money from the Fund. Each portfolio invests primarily in underlying equity and fixed-income index funds. Over time, the Fund's target allocations will gradually become more conservative."),
                                     p(strong("Which of these funds would you like to choose to invest your retirement money into?")),
                                     br(),
                                     radioButtons("choose_g4_q4_b", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_to_g4_q4_c', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 4-C"), value = "Group 4 Question 4-C1",
                                     br(),
                                     p("In the previous question, the gross expense ratio or implied gross ratio had been increased for the fund you had chosen earlier.  As a result, you chose a different fund to invest your retirement funds.  "),
                                     p("What is the lowest gross expense ratio on your originally selected fund where you would not have switched your fund choice?  "),
                                     uiOutput("slider_g4_q4_c1"),
                                     br(),
                                     actionButton('button_g4_c1_to_background_info', 'Confirm')
                            ),
                            
                            tabPanel(title = strong("Question 4-C"), value = "Group 4 Question 4-C2",
                                     br(),
                                     p("In the previous question, the gross expense ratio or implied gross expense ratio had been increased for the fund you had chosen earlier.  You continued to choose the fund that you had originally selected."),
                                     p("What is the highest gross expense ratio on your originally selected fund where you would have switched your fund choice?  "),
                                     uiOutput("slider_g4_q4_c2"),
                                     br(),
                                     p(strong("To which fund would you have switched? ")),
                                     br(),
                                     radioButtons("choose_g4_q4_c2", label = NULL,
                                                  c("Fund A", "Fund B", "Fund C"), selected = character(0)),
                                     br(),
                                     actionButton('button_g4_c2_to_background_info', 'Confirm')
                            )
                          )
                 )# end of tabpanel
                 
      )# end of navbarMenu (group 4)
      
    },
    
    # Background Information ---------------------------------------
    tabPanel(title = "Background Information", value = "Background Information",
             h3("Background Questions"),
             
             br(),
             p("1. What is your gender?"),
             radioButtons("choose_add_q1", label = NULL,
                          c("Male", "Female", "Non-binary"), selected = character(0)),
             
             br(),
             p("2. What is your race/ethnicity?"),
             radioButtons("choose_add_q2", label = NULL,
                          c("African-American", "Asian", "Caucasian – not Hispanic", "Hispanic", "Multi-racial", "Native Hawaiian or other Pacific Islander", "American Indian or Alaska Native", "Prefer not to Answer"), selected = character(0)),
             
             br(),
             p("3. What is your current marital status?"),
             radioButtons("choose_add_q3", label = NULL,
                          c("Never been married", "Married", "Separated", "Divorced", "Widow or Widower"), selected = character(0)),
             
             br(),
             p("4. What is the highest level of education you have completed?"),
             radioButtons("choose_add_q4", label = NULL,
                          c("Did not complete high school", "High school graduate or GED equivalent", "Some college, no degree", "Associate’s degree", "Bachelor’s degree", "Master’s degree", "Doctoral degree"), selected = character(0)),
             
             br(),
             # p("5. Which of the following best describes your role at the University of Pennsylvania?"),
             # radioButtons("choose_add_q5", label = NULL,
             #              c("Not Associated with the University of Pennsylvania", "Undergraduate student", "Master’s student", "Doctoral student", "Staff", "Faculty", "Other"), selected = character(0)),
             # 
             # br(),
             p("5. Have you ever held a job where your employer offered a retirement benefit package that required you to make retirement investment decisions?"),
             radioButtons("choose_add_q6", label = NULL,
                          c("Yes", "No"), selected = character(0)),
             
             br(),
             p("6. Have you ever made retirement investment decisions more generally, either for yourself or another individual?"),
             radioButtons("choose_add_q7", label = NULL,
                          c("Yes", "No"), selected = character(0)),
             
             br(),
             p("7. How sure do you feel about your ability to manage your own finances?"),
             radioButtons("choose_add_q8", label = NULL,
                          c("Not sure at all – I wish I knew a lot more about money management", "Not too sure – I wish I knew more about money management", "Somewhat sure – I understand most of what I will need to know", "Very sure – I understand money management very well"), 
                          selected = character(0), width = '100%'),
             
             br(),
             p("8. Which of the following statements comes closest to describing the amount of financial risk that you are willing to take when you save or make investments?"),
             radioButtons("choose_add_q9", label = NULL,
                          c("I am not willing to take any financial risk", "I prefer safer investments but do to take some financial risks.", "I take average financial risks expecting to earn average returns", "I take above average financial risk expecting to earn above average returns", "I take substantial financial risks expecting to earn substantial returns"), 
                          selected = character(0), width = '100%'),
             
             br(),
             p("9. This question is hypothetical. Imagine that, in addition to whatever you own, you have been given $1000.  Please choose between the following options:"),
             radioButtons("choose_add_q10", label = NULL,
                          c("A sure gain of $500", "Gaining $1000 with probability 50% and gaining $0 with probability 50%"), 
                          selected = character(0), width = '100%'),
             
             br(),
             p("10. This question is hypothetical. Imagine that, in addition to whatever you own, you have been given $2000. Please choose between the following options:"),
             radioButtons("choose_add_q11", label = NULL,
                          c("A sure loss of $500", "Losing $1000 with probability 50% and losing $0 with probability 50%"), 
                          selected = character(0), width = '100%'),
             
             br(),
             p("11. This question is hypothetical. Imagine that, in addition to whatever you own, you have been given $2000. Please choose between the following options:"),
             radioButtons("choose_add_q11a", label = NULL,
                          c("A sure loss of $1500", "Losing $1000 with probability 50% and losing $0 with probability 50%"), # Attention
                          selected = character(0), width = '100%'),
             
             br(),
             p("13. This question is hypothetical. If you had the choice between the following scenarios, which would you choose?"),
             radioButtons("choose_add_q12", label = NULL,
                          c("A sure gain of $3000", "Gaining $4000 with probability 80% and gaining $0 with probability 20%"), 
                          selected = character(0), width = '100%'),
             br(),
             p("14. This question is hypothetical. If you had the choice between the following scenarios, which would you choose?"), #attention
             radioButtons("choose_add_q12a", label = NULL,
                          c("A sure gain of $3000", "Gaining $2000 with probability 80% and gaining $0 with probability 20%"), 
                          selected = character(0), width = '100%'),
             
             br(),
             p("15. This question is hypothetical. If you had the choice between the following scenarios, which would you choose?"),
             radioButtons("choose_add_q13", label = NULL,
                          c("A sure loss of $3000", "Losing $4000 with probability 80% and losing $0 with probability 20%"), 
                          selected = character(0), width = '100%'),
             
             # br(),
             # p("13. There are 10 choices given in row in the table below.  For each row, please choose which option you prefer:", strong("Option A or Option B.")),
             # 
             # DT::dataTableOutput('table_backgroundinfo'),
             
             br(),
             p("16. Suppose you had $100 in a savings account and the interest rate was 2% per year.  After 5 years, how much do you think you would have in the account if you left the money to grow?"),
             radioButtons("choose_add_q15", label = NULL,
                          c("More than $102", "Exactly $102", "Less than $102", "Do not know"), 
                          selected = character(0), width = '100%'),
             
             br(),
             p("17. Imagine that the interest rate on your savings account was 1% per year and inflation was 2% per year.  After 1 year, how much would you be able to buy with the money in this account?"),
             radioButtons("choose_add_q16", label = NULL,
                          c("More than today", "Exactly the same", "Less than today", "Do not know"), 
                          selected = character(0), width = '100%'),
             
             br(),
             div("18. You purchase an item which costs $1000.  To pay for this item, you are given the following two options:"),
             div(strong("(a)"), "Pay 12 monthly installments of $100 each"),
             div(strong("(b)"), "Borrow at a 20% annual interest rate and pay back $1200 a year from now."),
             div("Which is the cheaper offer?"),
             radioButtons("choose_add_q17", label = NULL,
                          c("Option (a)", "Option (b)", "They are the same", "Do not know"), 
                          selected = character(0), width = '100%'),
             
             br(),
             p("19. What do you think is the percent chance that", strong("12 months from now"), "the average interest rate on savings accounts will be", strong("higher"), "than it is now?"),
             radioButtons("choose_add_q18", label = NULL,
                          c("Less than 15% chance", "15% to 30% chance", "30% to 45% chance", "45% to 65% chance", "65% to 80% chance", "More than 80% chance"),
                          selected = character(0), width = '100%'),
             
             br(),
             p("20. What do you think is the percent chance that ", strong("12 months from now"), ", on average, stock prices in the U.S. will be", strong("higher"), "than they are now?"),
             radioButtons("choose_add_q19", label = NULL,
                          c("Less than 15% chance", "15% to 30% chance", "30% to 45% chance", "45% to 65% chance", "65% to 80% chance", "More than 80% chance"),
                          selected = character(0), width = '100%'),
             
             br(),
             p("21. Over the next 12 months, do you think that there will be inflation or deflation? (Note:  deflation is the opposite of inflation)"),
             radioButtons("choose_add_q20", label = NULL,
                          c("Inflation", "Deflation (the opposite of inflation)"),
                          selected = character(0), width = '100%'),
             
             br(),
             p("22. Over the next 12 months, what is the rate of inflation (or deflation) that you expect?  Please answer below using negative numbers for deflation."),
             sliderInput("choose_add_q21", label = "Inflation Expectations (%)", 
                         value = 0,
                         min = -15,
                         max = 15),
             
             br(),
             p("23. How sure do you feel about your ability to answer questions about Registered Indexed Linked Annuities (RILAs)?"),
             radioButtons("choose_add_q22", label = NULL,
                          c("Not sure at all – I wish I knew a lot more about RILAs", "Not too sure – I wish I knew more about RILAs", "Neutral – I am neither unsure or sure about my ability to answer questions on RILAs", "Somewhat sure – I understand most of what I need to know about RILAs", "Very sure – I understand RILAs very well"),
                          selected = character(0), width = '100%'),
             
             br(),
             
             br(),
             p("24. There are 11 final questions.  For each question, please choose which option you prefer:"),
             br(),
             radioButtons("choose_add_q23k", label = NULL,
                          c("0% chance of $200 and 100% chance of $160", "0% chance of $385 and 100% chance of $10"),
                          selected = character(0), width = '100%'),
             br(),
             radioButtons("choose_add_q23a", label = NULL,
                          c("10% chance of $200 and 90% chance of $160", "10% chance of $385 and 90% chance of $10"),
                          selected = character(0), width = '100%'),
             br(),
             radioButtons("choose_add_q23b", label = NULL,
                          c("20% chance of $200 and 80% chance of $160", "20% chance of $385 and 80% chance of $10"),
                          selected = character(0), width = '100%'),
             br(),
             radioButtons("choose_add_q23c", label = NULL,
                          c("30% chance of $200 and 70% chance of $160", "30% chance of $385 and 70% chance of $10"),
                          selected = character(0), width = '100%'),
             br(),
             radioButtons("choose_add_q23d", label = NULL,
                          c("40% chance of $200 and 60% chance of $160", "40% chance of $385 and 60% chance of $10"),
                          selected = character(0), width = '100%'),
             br(),
             radioButtons("choose_add_q23e", label = NULL,
                          c("50% chance of $200 and 50% chance of $160", "50% chance of $385 and 50% chance of $10"),
                          selected = character(0), width = '100%'),
             br(),
             radioButtons("choose_add_q23f", label = NULL,
                          c("60% chance of $200 and 40% chance of $160", "60% chance of $385 and 40% chance of $10"),
                          selected = character(0), width = '100%'),
             
             br(),
             radioButtons("choose_add_q23g", label = NULL,
                          c("70% chance of $200 and 30% chance of $160", "70% chance of $385 and 30% chance of $10"),
                          selected = character(0), width = '100%'),
             
             br(),
             radioButtons("choose_add_q23h", label = NULL,
                          c("80% chance of $200 and 20% chance of $160", "80% chance of $385 and 20% chance of $10"),
                          selected = character(0), width = '100%'),
             
             br(),
             radioButtons("choose_add_q23i", label = NULL,
                          c("90% chance of $200 and 10% chance of $160", "90% chance of $385 and 10% chance of $10"),
                          selected = character(0), width = '100%'),
             
             br(),
             radioButtons("choose_add_q23j", label = NULL,
                          c("100% chance of $200 and 0% chance of $160", "100% chance of $385 and 0% chance of $10"),
                          selected = character(0), width = '100%'),
             
             
             
             
             
             # v_backgroundinfo_a <- c("10% chance of $200 and 90% chance of $160",
             #                         "20% chance of $200 and 80% chance of $160",
             #                         "30% chance of $200 and 70% chance of $160",
             #                         "40% chance of $200 and 60% chance of $160",
             #                         "50% chance of $200 and 50% chance of $160",
             #                         "60% chance of $200 and 40% chance of $160",
             #                         "70% chance of $200 and 30% chance of $160",
             #                         "80% chance of $200 and 20% chance of $160",
             #                         "90% chance of $200 and 10% chance of $160",
             #                         "100% chance of $200 and 0% chance of $160")
             # 
             # v_backgroundinfo_b <- c("10% chance of $385 and 90% chance of $10",
             #                         "20% chance of $385 and 80% chance of $10",
             #                         "30% chance of $385 and 70% chance of $10",
             #                         "40% chance of $385 and 60% chance of $10",
             #                         "50% chance of $385 and 50% chance of $10",
             #                         "60% chance of $385 and 40% chance of $10",
             #                         "70% chance of $385 and 30% chance of $10",
             #                         "80% chance of $385 and 20% chance of $10",
             #                         "90% chance of $385 and 10% chance of $10",
             #                         "100% chance of $385 and 0% chance of $10")
             
             actionButton('button_to_last_page', 'Continue (please ensure all questions are answered)')
             # downloadButton('download', 'download')
    ),
    
    # Final Thank You ---------------------------------------
    tabPanel(title = "Thank You!", value = "Thank You",
             h3("Thank You!"),
             br(),
             h3("Your unique completion code is" , textOutput("comp_code", inline = TRUE)),
             br(),
             p("This study is concerned with the ability to make decisions regarding retirement investments. The study investigates how different levels of information and default choices affect retirement investment decision-making. The study also investigates how individuals understand registered index-linked annuities relative to target date mutual funds."),
             p(strong("How was this tested?")),
             p("In this study, you played the role of a company employee, and you were asked to select an investment for your retirement account. You were asked to choose among a choice of investments, each involving risk. All participants performed the same task. In performing the tasks, one group was given the same level of information as given to employees today when making retirement investments. This group was told one choice was the default investment chosen by the employer. A second group performed the task with the same information as the first group but were not told that one fund was pre-selected as a default investment. A third group was given the same information as the first group, but were also given, for each fund choice offered, an estimate of what their retirement benefit would be if they chose that fund and remain invested in it from now until their desired retirement age. A fourth group was given the same information as the second group and were also given the additional information about the projected retirement benefit that the third group received."),
             p(strong("Hypotheses and main questions:")),
             p("We expect that being given additional information about investment choices, in terms of how those choices would affect the resulting retirement benefit, will help individuals make better investment decisions. Within some of the choices offered, one choice dominated another one. We expect that participants are less likely to choose the dominated choice when given more information about the funds."),
             p("We should also find that being told which fund was pre-selected by the employer as the default investment will have an impact on investment choices. That is, we expect to find that participants are more likely to choose the default fund when they are informed there is a default."),
             p("The transparency of RILAs means they should be easier to compare to one another relative to a set of target-date funds, which have been shown in the literature to lack transparency. We expect that subjects will be better at identifying the optimal investment choice among RILAs. We expect to find that subjects will be more likely to choose a suboptimal fund among a menu of target-date funds. The menu set that includes both RILAs and target-date funds will help us better understand how having both options available impacts investment choice. It is unclear how individuals would respond to an insurance-related investment product, such as RILAs, so the choice made in this part of the experiment will provide some evidence on this question."),
             p(strong("Why is this important to study?")),
             p("The factors that affect an individual’s retirement investment choice are relevant to sound retirement planning. The questions of how and why people to choose to invest in certain assets and accrue certain levels of risk are very much open to psychological research."),
             p(strong("What if I want to know more?")),
             p("If you are interested in learning more about registered index-linked securities, please consult:"),
             p(HTML("<ul><li>Moenig, Thorsten. 2022. It's RILA time: An introduction to registered index‐linked annuities. Journal of Risk and Insurance, 89(2), pp.339-369.</li></ul>")),
             p("If you would like to receive a report of this research when it is completed (or a summary of the findings), please contact Jacqueline Volkman-Wise at jvwise@wharton.upenn.edu."),
             p("If you have concerns about your rights as a participant in this experiment, please contact the Institutional Review Board at (215) 898-2614."),
             h3("Thank you again for your participation!")
    )
  ),
  useShinyjs()
  #### "ui" ends here ####
)

# server  ---------------------------------------
server <- function(input, output, session){
  
  # Function to get retire year ---------------------------------
  year_retire <- reactive(
    {
      min(as.numeric(input$age_retire) - as.numeric(input$age_current) + 2022, 2072)  
    }
  )
  
  cost_alt <- reactive({
    cs$Cost_grid[gross_expense_ratio_random]*100
  }
  )
  
  # Sliders ----------------------------------------------
  output$slider_g1_q1_c1 <- renderUI({
    sliderInput("g1_q1_c1", min=.2, max=cost_alt(), value=.2, label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g1_q1_c2 <- renderUI({
    sliderInput("g1_q1_c2", min=cost_alt(), max= 1.5, value=cost_alt(), label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g1_q2_c1 <- renderUI({
    sliderInput("g1_q2_c1", min=.2, max=cost_alt(), value=.2, label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g1_q2_c2 <- renderUI({
    sliderInput("g1_q2_c2", min=cost_alt(), max= 1.5, value=cost_alt(), label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g1_q3_c1 <- renderUI({
    sliderInput("g1_q3_c1", min=.2, max=cost_alt(), value=.2, label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g1_q3_c2 <- renderUI({
    sliderInput("g1_q3_c2", min=cost_alt(), max= 1.5, value=cost_alt(), label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g1_q4_c1 <- renderUI({
    sliderInput("g1_q4_c1", min=.2, max=cost_alt(), value=.2, label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g1_q4_c2 <- renderUI({
    sliderInput("g1_q4_c2", min=cost_alt(), max= 1.5, value=cost_alt(), label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g2_q1_c1 <- renderUI({
    sliderInput("g2_q1_c1", min=.2, max=cost_alt(), value=.2, label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g2_q1_c2 <- renderUI({
    sliderInput("g2_q1_c2", min=cost_alt(), max= 1.5, value=cost_alt(), label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g2_q2_c1 <- renderUI({
    sliderInput("g2_q2_c1", min=.2, max=cost_alt(), value=.2, label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g2_q2_c2 <- renderUI({
    sliderInput("g2_q2_c2", min=cost_alt(), max= 1.5, value=cost_alt(), label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g2_q3_c1 <- renderUI({
    sliderInput("g2_q3_c1", min=.2, max=cost_alt(), value=.2, label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g2_q3_c2 <- renderUI({
    sliderInput("g2_q3_c2", min=cost_alt(), max= 1.5, value=cost_alt(), label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g2_q4_c1 <- renderUI({
    sliderInput("g2_q4_c1", min=.2, max=cost_alt(), value=.2, label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g2_q4_c2 <- renderUI({
    sliderInput("g2_q4_c2", min=cost_alt(), max= 1.5, value=cost_alt(), label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g3_q1_c1 <- renderUI({
    sliderInput("g3_q1_c1", min=.2, max=cost_alt(), value=.2, label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g3_q1_c2 <- renderUI({
    sliderInput("g3_q1_c2", min=cost_alt(), max= 1.5, value=cost_alt(), label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g3_q2_c1 <- renderUI({
    sliderInput("g3_q2_c1", min=.2, max=cost_alt(), value=.2, label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g3_q2_c2 <- renderUI({
    sliderInput("g3_q2_c2", min=cost_alt(), max= 1.5, value=cost_alt(), label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g3_q3_c1 <- renderUI({
    sliderInput("g3_q3_c1", min=.2, max=cost_alt(), value=.2, label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g3_q3_c2 <- renderUI({
    sliderInput("g3_q3_c2", min=cost_alt(), max= 1.5, value=cost_alt(), label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g3_q4_c1 <- renderUI({
    sliderInput("g3_q4_c1", min=.2, max=cost_alt(), value=.2, label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g3_q4_c2 <- renderUI({
    sliderInput("g3_q4_c2", min=cost_alt(), max= 1.5, value=cost_alt(), label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g4_q1_c1 <- renderUI({
    sliderInput("g4_q1_c1", min=.2, max=cost_alt(), value=.2, label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g4_q1_c2 <- renderUI({
    sliderInput("g4_q1_c2", min=cost_alt(), max= 1.5, value=cost_alt(), label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g4_q2_c1 <- renderUI({
    sliderInput("g4_q2_c1", min=.2, max=cost_alt(), value=.2, label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g4_q2_c2 <- renderUI({
    sliderInput("g4_q2_c2", min=cost_alt(), max= 1.5, value=cost_alt(), label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g4_q3_c1 <- renderUI({
    sliderInput("g4_q3_c1", min=.2, max=cost_alt(), value=.2, label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g4_q3_c2 <- renderUI({
    sliderInput("g4_q3_c2", min=cost_alt(), max= 1.5, value=cost_alt(), label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g4_q4_c1 <- renderUI({
    sliderInput("g4_q4_c1", min=.2, max=cost_alt(), value=.2, label = "Gross Expense Ratio (in %)")
  })
  
  output$slider_g4_q4_c2 <- renderUI({
    sliderInput("g4_q4_c2", min=cost_alt(), max= 1.5, value=cost_alt(), label = "Gross Expense Ratio (in %)")
  })
  
  
  # Practice Question Stuff -----------------------------------------------
  
  
  #   
  # p(strong("2. The index return is -20%.  What is the return you earn on the RILA?")),
  # radioButtons("choose_practice_q3_2", label = NULL,
  #              c("0%", "10%", "-5%", "-15%", "-20%"),selected = character(0)),
  # br(),
  # h4("Practice Questions for RILA with Buffer"),
  # p("Suppose you invest in a RILA that has a buffer of -10% and a cap of 12%.  Please answer the following questions."),
  # p(strong("1. The index return is 10%.  What is the return you earn on the RILA?")),
  # radioButtons("choose_practice_q3_3", label = NULL,
  #              c("0%", "2%", "10%", "12%", "22%"),selected = character(0)),
  # p(strong("2. The index return is -15%.  What is the return you earn on the RILA?")),
  # radioButtons("choose_practice_q3_4", label = NULL,
  #              c("0%", "-3%", "-5%", "-15%", "-25%"),selected = character(0)),
  # br(),
  # actionButton('button_to_practice_q4', 'Confirm')
  
  
  # Function to get income bins for base cost -----------------------
  vbins_1 <- reactive(
    {
      age_current <- as.numeric(input$age_current)
      age_retire <- as.numeric(input$age_retire)
      retire_plan <- as.numeric(input$retire_plan)
      salary_current <- as.numeric(input$salary_current)
      
      if(age_retire < (age_current + 2)){
        age_retire = age_current + 2
      }
      
      # age_current <- 20
      # age_retire <- 50
      # retire_plan <- 50
      # salary_current <- 0
      
      vbins <- Get_V_bins(CurrentAge = age_current,
                          CurrentAccountValue = retire_plan,
                          InitialContributionAmount = salary_current * .1,
                          RetirementAge = age_retire,
                          AnnualCost = cs$Cost_grid[1],
                          logR = lr$logR,
                          r = lr$r,
                          infl = lr$infl,
                          Cost_grid = cs$Cost_grid,
                          con_type_vec = cs$con_type_vec,
                          con_spec_vec = cs$con_spec_vec,
                          con_cap_vec = cs$con_cap_vec)
      
      # vbins <- Get_V_bins(CurrentAge = 0,
      #                     CurrentAccountValue = 0,
      #                     InitialContributionAmount = 0 * .1,
      #                     RetirementAge = 1,
      #                     AnnualCost = cs$Cost_grid[1],
      #                     logR = lr$logR,
      #                     r = lr$r,
      #                     infl = lr$infl,
      #                     Cost_grid = cs$Cost_grid,
      #                     con_type_vec = cs$con_type_vec,
      #                     con_spec_vec = cs$con_spec_vec,
      #                     con_cap_vec = cs$con_cap_vec)
      return(vbins)
    }
  )
  
  # Function to get income bins for alt cost ------------------------------
  vbins_alt <- reactive(
    {
      age_current <- as.numeric(input$age_current)
      age_retire <- as.numeric(input$age_retire)
      retire_plan <- as.numeric(input$retire_plan)
      salary_current <- as.numeric(input$salary_current)
      
      if(age_retire < (age_current + 2)){
        age_retire = age_current + 2
      }
      
      vbins <- Get_V_bins(CurrentAge = age_current,
                          CurrentAccountValue = retire_plan,
                          InitialContributionAmount = salary_current * .1,
                          RetirementAge = age_retire,
                          AnnualCost = cs$Cost_grid[gross_expense_ratio_random],
                          logR = lr$logR,
                          r = lr$r,
                          infl = lr$infl,
                          Cost_grid = cs$Cost_grid,
                          con_type_vec = cs$con_type_vec,
                          con_spec_vec = cs$con_spec_vec,
                          con_cap_vec = cs$con_cap_vec)
      return(vbins)
    }
  )
  
  
  # Functions to get caps after expense ratio change  ---------------------------------------
  
  #Target data RILAS (with FLOORS): Fund A 
  update_current_cap_g1_q2_funda <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[start_year, gross_expense_ratio_random, 7])
    }
  )
  
  update_middle_cap_g1_q2_funda <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[ round(median(50 - start_year)) , gross_expense_ratio_random,7])
    }
  )
  update_final_cap_g1_q2_funda <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[ 50, gross_expense_ratio_random,7])
    }
  )
  
  #Target data RILAS (with FLOORS): Fund B
  update_current_cap_g1_q2_fundb <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[start_year, gross_expense_ratio_random, 8])
    }
  )
  
  update_middle_cap_g1_q2_fundb <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[round(median(50 - start_year)), gross_expense_ratio_random, 8])
    }
  )
  update_final_cap_g1_q2_fundb <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[50, gross_expense_ratio_random, 8])
    }
  )
  
  #Target data RILAS (with FLOORS): Fund C
  update_current_cap_g1_q2_fundc <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[start_year, gross_expense_ratio_random,9 ])
    }
  )
  
  update_middle_cap_g1_q2_fundc <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[round(median(50 - start_year)), gross_expense_ratio_random, 9])
    }
  )
  update_final_cap_g1_q2_fundc <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[50, gross_expense_ratio_random, 9])
    }
  )
  
  #Target data RILAS (with BUFFERS): Fund A
  update_current_cap_g1_q3_funda <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[start_year, gross_expense_ratio_random, 4])
    }
  )
  
  update_middle_cap_g1_q3_funda <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[round(median(50 - start_year)), gross_expense_ratio_random, 4])
    }
  )
  update_final_cap_g1_q3_funda <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[50, gross_expense_ratio_random, 4])
    }
  )
  
  #Target data RILAS (with BUFFERS): Fund B
  update_current_cap_g1_q3_fundb <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[start_year, gross_expense_ratio_random, 5])
    }
  )
  
  update_middle_cap_g1_q3_fundb <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[round(median(50 - start_year)), gross_expense_ratio_random, 5])
    }
  )
  update_final_cap_g1_q3_fundb <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[50, gross_expense_ratio_random, 5])
    }
  )
  
  #Target data RILAS (with BUFFERS): Fund C
  update_current_cap_g1_q3_fundc <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[start_year, gross_expense_ratio_random,6])
    }
  )
  
  update_middle_cap_g1_q3_fundc <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[ round(median(50 - start_year)), gross_expense_ratio_random,6])
    }
  )
  update_final_cap_g1_q3_fundc <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      return(cs$con_cap_vec[50, gross_expense_ratio_random,6 ])
    }
  )
  
  # Functions to get income bins for alt cost --------------------------------------------
  
  #Target date fund: Fund A
  update_retirebenefit_worst_g3_q1_funda <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[1, 1])
    }
  )
  
  update_retirebenefit_expected_g3_q1_funda <- reactive(
    {
      vbins <- vbins_alt()
      return(mean(vbins[1, 2:11]))
    }
  )
  
  update_retirebenefit_best_g3_q1_funda <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[1, 12])
    }
  )
  
  update_retirebenefit_sd_g3_q1_funda <- reactive(
    {
      vbins <- vbins_alt()
      return(sd(vbins[1, 2:11]))
    }
  )
  
  
  #Target date fund: Fund B
  update_retirebenefit_worst_g3_q1_fundb <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[2, 1])
    }
  )
  update_retirebenefit_expected_g3_q1_fundb <- reactive(
    {
      vbins <- vbins_alt()
      return(mean(vbins[2, 2:11]))
    }
  )
  update_retirebenefit_best_g3_q1_fundb <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[2, 12])
    }
  )
  
  update_retirebenefit_sd_g3_q1_fundb <- reactive(
    {
      vbins <- vbins_alt()
      return(sd(vbins[2, 2:11]))
    }
  )
  
  
  #Target date fund: Fund C
  update_retirebenefit_worst_g3_q1_fundc <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[3, 1])
    }
  )
  update_retirebenefit_expected_g3_q1_fundc <- reactive(
    {
      vbins <- vbins_alt()
      return(mean(vbins[3, 2:11]))
    }
  )
  update_retirebenefit_best_g3_q1_fundc <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[3, 12])
    }
  )
  
  update_retirebenefit_sd_g3_q1_fundc <- reactive(
    {
      vbins <- vbins_alt()
      return(sd(vbins[3, 2:11]))
    }
  )
  
  
  #Target date RILA (with FLOORS): Fund A
  update_retirebenefit_worst_g3_q2_funda <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[7, 1])
    }
  )
  update_retirebenefit_expected_g3_q2_funda <- reactive(
    {
      vbins <- vbins_alt()
      return(mean(vbins[7, 2:11]))
    }
  )
  update_retirebenefit_best_g3_q2_funda <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[7, 12])
    }
  )
  
  update_retirebenefit_sd_g3_q2_funda <- reactive(
    {
      vbins <- vbins_alt()
      return(sd(vbins[7, 2:11]))
    }
  )
  
  
  #Target date RILA (with FLOORS): Fund B
  update_retirebenefit_worst_g3_q2_fundb <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[8, 1])
    }
  )
  update_retirebenefit_expected_g3_q2_fundb <- reactive(
    {
      vbins <- vbins_alt()
      return(mean(vbins[8, 2:11]))
    }
  )
  update_retirebenefit_best_g3_q2_fundb <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[8, 12])
    }
  )
  
  update_retirebenefit_sd_g3_q2_fundb <- reactive(
    {
      vbins <- vbins_alt()
      return(sd(vbins[8, 2:11]))
    }
  )
  
  
  #Target date RILA (with FLOORS): Fund C
  update_retirebenefit_worst_g3_q2_fundc <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[9, 1])
    }
  )
  update_retirebenefit_expected_g3_q2_fundc <- reactive(
    {
      vbins <- vbins_alt()
      return(mean(vbins[9, 2:11]))
    }
  )
  update_retirebenefit_best_g3_q2_fundc <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[9, 12])
    }
  )
  
  update_retirebenefit_sd_g3_q2_fundc <- reactive(
    {
      vbins <- vbins_alt()
      return(sd(vbins[9, 2:11]))
    }
  )
  
  
  #Target date RILA (with BUFFERS): Fund A
  update_retirebenefit_worst_g3_q3_funda <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[4, 1])
    }
  )
  update_retirebenefit_expected_g3_q3_funda <- reactive(
    {
      vbins <- vbins_alt()
      return(mean(vbins[4, 2:11]))
    }
  )
  update_retirebenefit_best_g3_q3_funda <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[4, 12])
    }
  )
  
  update_retirebenefit_sd_g3_q3_funda <- reactive(
    {
      vbins <- vbins_alt()
      return(sd(vbins[4, 2:11]))
    }
  )
  
  
  #Target date RILA (with BUFFERS): Fund B
  update_retirebenefit_worst_g3_q3_fundb <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[5, 1])
    }
  )
  update_retirebenefit_expected_g3_q3_fundb <- reactive(
    {
      vbins <- vbins_alt()
      return(mean(vbins[5, 2:11]))
    }
  )
  update_retirebenefit_best_g3_q3_fundb <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[5, 12])
    }
  )
  
  update_retirebenefit_sd_g3_q3_fundb <- reactive(
    {
      vbins <- vbins_alt()
      return(sd(vbins[5, 2:11]))
    }
  )
  
  
  #Target date RILA (with BUFFERS): Fund C
  update_retirebenefit_worst_g3_q3_fundc <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[6, 1])
    }
  )
  update_retirebenefit_expected_g3_q3_fundc <- reactive(
    {
      vbins <- vbins_alt()
      return(mean(vbins[6, 2:11]))
    }
  )
  update_retirebenefit_best_g3_q3_fundc <- reactive(
    {
      vbins <- vbins_alt()
      return(vbins[6, 12])
    }
  )
  
  update_retirebenefit_sd_g3_q3_fundc <- reactive(
    {
      vbins <- vbins_alt()
      return(sd(vbins[6, 2:11]))
    }
  )
  
  
  # Repeating outcome year retire -------------------------------------------
  output$year_retire_g1_q1_a <- renderText(paste0(year_retire(), "."))
  output$year_retire_g1_q1_b <- renderText(paste0(year_retire(), "."))
  output$year_retire_g1_q2_a <- renderText(paste0(year_retire(), "."))
  output$year_retire_g1_q2_b <- renderText(paste0(year_retire(), "."))
  output$year_retire_g1_q3_a <- renderText(paste0(year_retire(), "."))
  output$year_retire_g1_q3_b <- renderText(paste0(year_retire(), "."))
  output$year_retire_g1_q4_a <- renderText(paste0(year_retire(), "."))
  output$year_retire_g1_q4_b <- renderText(paste0(year_retire(), "."))
  output$year_retire_g2_q1_a <- renderText(paste0(year_retire(), "."))
  output$year_retire_g2_q1_b <- renderText(paste0(year_retire(), "."))
  output$year_retire_g2_q2_a <- renderText(paste0(year_retire(), "."))
  output$year_retire_g2_q2_b <- renderText(paste0(year_retire(), "."))
  output$year_retire_g2_q3_a <- renderText(paste0(year_retire(), "."))
  output$year_retire_g2_q3_b <- renderText(paste0(year_retire(), "."))
  output$year_retire_g2_q4_a <- renderText(paste0(year_retire(), "."))
  output$year_retire_g2_q4_b <- renderText(paste0(year_retire(), "."))
  output$year_retire_g3_q1_a <- renderText(paste0(year_retire(), "."))
  output$year_retire_g3_q1_b <- renderText(paste0(year_retire(), "."))
  output$year_retire_g3_q2_a <- renderText(paste0(year_retire(), "."))
  output$year_retire_g3_q2_b <- renderText(paste0(year_retire(), "."))
  output$year_retire_g3_q3_a <- renderText(paste0(year_retire(), "."))
  output$year_retire_g3_q3_b <- renderText(paste0(year_retire(), "."))
  output$year_retire_g3_q4_a <- renderText(paste0(year_retire(), "."))
  output$year_retire_g3_q4_b <- renderText(paste0(year_retire(), "."))
  output$year_retire_g4_q1_a <- renderText(paste0(year_retire(), "."))
  output$year_retire_g4_q1_b <- renderText(paste0(year_retire(), "."))
  output$year_retire_g4_q2_a <- renderText(paste0(year_retire(), "."))
  output$year_retire_g4_q2_b <- renderText(paste0(year_retire(), "."))
  output$year_retire_g4_q3_a <- renderText(paste0(year_retire(), "."))
  output$year_retire_g4_q3_b <- renderText(paste0(year_retire(), "."))
  output$year_retire_g4_q4_a <- renderText(paste0(year_retire(), "."))
  output$year_retire_g4_q4_b <- renderText(paste0(year_retire(), "."))
  
  # Functions to disable jumping using tabs ---------------------------------------
  shinyjs::disable(selector = '#barlist li a')
  # shinyjs::disable(selector = '#barlist_g1_q1 li a')
  shinyjs::disable(selector = '#barlist_g1_q2 li a')
  shinyjs::disable(selector = '#barlist_g1_q3 li a')
  shinyjs::disable(selector = '#barlist_g1_q4 li a')
  shinyjs::disable(selector = '#barlist_g2_q1 li a')
  shinyjs::disable(selector = '#barlist_g2_q2 li a')
  shinyjs::disable(selector = '#barlist_g2_q3 li a')
  shinyjs::disable(selector = '#barlist_g2_q4 li a')
  shinyjs::disable(selector = '#barlist_g3_q1 li a')
  shinyjs::disable(selector = '#barlist_g3_q2 li a')
  shinyjs::disable(selector = '#barlist_g3_q3 li a')
  shinyjs::disable(selector = '#barlist_g3_q4 li a')
  shinyjs::disable(selector = '#barlist_g4_q1 li a')
  shinyjs::disable(selector = '#barlist_g4_q2 li a')
  shinyjs::disable(selector = '#barlist_g4_q3 li a')
  shinyjs::disable(selector = '#barlist_g4_q4 li a')
  
  # Functions for buttons ---------------------------------------
  # Buttons: before survey ---------------------------------------
  observeEvent(input$button_agree_survey, {
    updateTabsetPanel(session, "barlist", selected = "Additional Questions")
  })

  observeEvent(input$button_to_q1, {
    if(isTruthy(input$worker_ID))
    {
    updateTabsetPanel(session, "barlist", selected = "Background")
    }
  })
  
  observeEvent(input$button_to_practice_q1, {
    updateTabsetPanel(session, "barlist", selected = "Practice Question 1")
  })
  
  # observeEvent(input$button_to_practice_q2, {
  #   if (is.null(input$choose_practice_q1)) {
  #     updateTabsetPanel(session, "barlist", selected = "Practice Question 2")
  #   }
  # })
  
  observeEvent(input$button_to_practice_q4, {
    if (isTruthy(input$choose_practice_q1)) {
      updateTabsetPanel(session, "barlist", selected = "Practice Question 4")
    }
  })
  
  observeEvent(input$button_to_practice_q3, {
    if (isTruthy(input$choose_practice_q1)) {
      updateTabsetPanel(session, "barlist", selected = "Practice Question 3")
    }
  })
  
  observeEvent(input$button_to_practice_q3a, {
    if (isTruthy(input$choose_practice_q3_1) & isTruthy(input$choose_practice_q3_2) & isTruthy(input$choose_practice_q3_3) & isTruthy(input$choose_practice_q3_4)) {
      updateTabsetPanel(session, "barlist", selected = "Practice Question 3 Answers")
    }
  })
  
  observeEvent(input$button_to_practice_q5, {
    if (isTruthy(input$choose_practice_q4)) {
      updateTabsetPanel(session, "barlist", selected = "Practice Question 5")
    }
  })
  
  observeEvent(input$button_to_confirm_instruction, {
    if (isTruthy(input$choose_practice_q5)) {
      updateTabsetPanel(session, "barlist", selected = "Confirm the Participation")
    }
  })
  
  # observeEvent(input$button_to_additional_info, {
  #   if (isTruthy(input$checkbox_understandsurvey)) {
  #     updateTabsetPanel(session, "barlist", selected = "Additional Questions")
  #   }
  # })
  
  observeEvent(input$button_to_additional_info, {
    
    if (group_assigned == 1) {
      updateTabsetPanel(session, "barlist", selected = "Group 1 Question 1")
      hideTab("barlist_g4_q1", target = "Group 1 Question 1-C2")
    } else if (group_assigned == 2) {
      updateTabsetPanel(session, "barlist", selected = "Group 2 Question 1")
      hideTab("barlist_g2_q1", target = "Group 2 Question 1-C2")
    } else if (group_assigned == 3) {
      updateTabsetPanel(session, "barlist", selected = "Group 3 Question 1")
      hideTab("barlist_g3_q1", target = "Group 3 Question 1-C2")
    } else {
      updateTabsetPanel(session, "barlist", selected = "Group 4 Question 1")
      hideTab("barlist_g4_q1", target = "Group 4 Question 1-C2")
    }
    
  }) #at the bottom of page "additional questions"
  
  # Buttons: Group 1 Question 1 ---------------------------------------
  observeEvent(input$button_to_g1_q1_b, {
    if (isTruthy(input$choose_g1_q1_a)) {
      updateTabsetPanel(session, "barlist_g1_q1", selected = "Group 1 Question 1-B")
    }
  })
  
  observeEvent(input$button_to_g1_q1_c, {
    if (isTruthy(input$choose_g1_q1_b)) {
      
      if (input$choose_g1_q1_a == input$choose_g1_q1_b) {
        hideTab("barlist_g1_q1", target = "Group 1 Question 1-C1")
        showTab("barlist_g1_q1", target = "Group 1 Question 1-C2")
        updateTabsetPanel(session, "barlist_g1_q1", selected = "Group 1 Question 1-C2")
      } else {
        updateTabsetPanel(session, "barlist_g1_q1", selected = "Group 1 Question 1-C1")
      }
    }
  })
  
  observeEvent(input$button_c1_to_g1_q2, {
    if (isTruthy(input$g1_q1_c1)) {
      updateTabsetPanel(session, "barlist", selected = "Group 1 Question 2")
      hideTab("barlist_g1_q2", target = "Group 1 Question 2-C2")
    }
  })
  
  observeEvent(input$button_c2_to_g1_q2, {
    if (isTruthy(input$choose_g1_q1_c2) & isTruthy(input$g1_q1_c2)) {
      updateTabsetPanel(session, "barlist", selected = "Group 1 Question 2")
      hideTab("barlist_g1_q2", target = "Group 1 Question 2-C2")
    }
  })
  
  # Buttons: Group 1 Question 2 ---------------------------------------
  observeEvent(input$button_to_g1_q2_b, {
    if (isTruthy(input$choose_g1_q2_a)) {
      updateTabsetPanel(session, "barlist_g1_q2", selected = "Group 1 Question 2-B")
    }
  })
  
  observeEvent(input$button_to_g1_q2_c, {
    if (isTruthy(input$choose_g1_q2_b)) {
      
      if (input$choose_g1_q2_a == input$choose_g1_q2_b) {
        hideTab("barlist_g1_q2", target = "Group 1 Question 2-C1")
        showTab("barlist_g1_q2", target = "Group 1 Question 2-C2")
        updateTabsetPanel(session, "barlist_g1_q2", selected = "Group 1 Question 2-C2")
      } else {
        updateTabsetPanel(session, "barlist_g1_q2", selected = "Group 1 Question 2-C1")
      }
    } 
  })
  
  observeEvent(input$button_c1_to_g1_q3, {
    if (isTruthy(input$g1_q2_c1)) {
      updateTabsetPanel(session, "barlist", selected = "Group 1 Question 3")
      hideTab("barlist_g1_q3", target = "Group 1 Question 3-C2")
    }
  })
  
  observeEvent(input$button_c2_to_g1_q3, {
    if (isTruthy(input$choose_g1_q2_c2) & isTruthy(input$g1_q2_c2)) {
      updateTabsetPanel(session, "barlist", selected = "Group 1 Question 3")
      hideTab("barlist_g1_q3", target = "Group 1 Question 3-C2")
    }
  })
  
  # Buttons: Group 1 Question 3 ---------------------------------------
  observeEvent(input$button_to_g1_q3_b, {
    if (isTruthy(input$choose_g1_q3_a)) {
      updateTabsetPanel(session, "barlist_g1_q3", selected = "Group 1 Question 3-B")
    }
  })
  
  observeEvent(input$button_to_g1_q3_c, {
    if (isTruthy(input$choose_g1_q3_b)) {
      
      if (input$choose_g1_q3_a == input$choose_g1_q3_b) {
        hideTab("barlist_g1_q3", target = "Group 1 Question 3-C1")
        showTab("barlist_g1_q3", target = "Group 1 Question 3-C2")
        updateTabsetPanel(session, "barlist_g1_q3", selected = "Group 1 Question 3-C2")
      } else {
        updateTabsetPanel(session, "barlist_g1_q3", selected = "Group 1 Question 3-C1")
      }
    } 
  })
  
  observeEvent(input$button_c1_to_g1_q4, {
    if (isTruthy(input$g1_q3_c1)) {
      updateTabsetPanel(session, "barlist", selected = "Group 1 Question 4")
      hideTab("barlist_g1_q4", target = "Group 1 Question 4-C2")
    }
  })
  
  observeEvent(input$button_c2_to_g1_q4, {
    if (isTruthy(input$choose_g1_q3_c2) & isTruthy(input$g1_q3_c2)) {
      updateTabsetPanel(session, "barlist", selected = "Group 1 Question 4")
      hideTab("barlist_g1_q4", target = "Group 1 Question 4-C2")
    }
  })
  
  # Buttons: Group 1 Question 4 ---------------------------------------
  observeEvent(input$button_to_g1_q4_b, {
    if (isTruthy(input$choose_g1_q4_a)) {
      updateTabsetPanel(session, "barlist_g1_q4", selected = "Group 1 Question 4-B")
    }
  })
  
  observeEvent(input$button_to_g1_q4_c, {
    if (isTruthy(input$choose_g1_q4_b)) {
      
      if (input$choose_g1_q4_a == input$choose_g1_q4_b) {
        hideTab("barlist_g1_q4", target = "Group 1 Question 4-C1")
        showTab("barlist_g1_q4", target = "Group 1 Question 4-C2")
        updateTabsetPanel(session, "barlist_g1_q4", selected = "Group 1 Question 4-C2")
      } else {
        updateTabsetPanel(session, "barlist_g1_q4", selected = "Group 1 Question 4-C1")
      }
    } 
  })
  
  observeEvent(input$button_g1_c1_to_background_info, {
    if (isTruthy(input$g1_q4_c1)) {
      updateTabsetPanel(session, "barlist", selected = "Background Information")
    }
  })
  
  observeEvent(input$button_g1_c2_to_background_info, {
    if (isTruthy(input$choose_g1_q4_c2) & isTruthy(input$g1_q4_c2)) {
      updateTabsetPanel(session, "barlist", selected = "Background Information")
    }
  })
  
  # Buttons: Group 2 Question 1 ---------------------------------------
  observeEvent(input$button_to_g2_q1_b, {
    if (isTruthy(input$choose_g2_q1_a)) {
      updateTabsetPanel(session, "barlist_g2_q1", selected = "Group 2 Question 1-B")
    }
  })
  
  observeEvent(input$button_to_g2_q1_c, {
    if (isTruthy(input$choose_g2_q1_b)) {
      
      if (input$choose_g2_q1_a == input$choose_g2_q1_b) {
        hideTab("barlist_g2_q1", target = "Group 2 Question 1-C1")
        showTab("barlist_g2_q1", target = "Group 2 Question 1-C2")
        updateTabsetPanel(session, "barlist_g2_q1", selected = "Group 2 Question 1-C2")
      } else {
        updateTabsetPanel(session, "barlist_g2_q1", selected = "Group 2 Question 1-C1")
      }
    }
  })
  
  observeEvent(input$button_c1_to_g2_q2, {
    if (isTruthy(input$g2_q1_c1)) {
      updateTabsetPanel(session, "barlist", selected = "Group 2 Question 2")
      hideTab("barlist_g2_q2", target = "Group 2 Question 2-C2")
    }
  })
  
  observeEvent(input$button_c2_to_g2_q2, {
    if (isTruthy(input$choose_g2_q1_c2) & isTruthy(input$g2_q1_c2)) {
      updateTabsetPanel(session, "barlist", selected = "Group 2 Question 2")
      hideTab("barlist_g2_q2", target = "Group 2 Question 2-C2")
    }
  })
  
  # Buttons: Group 2 Question 2 ---------------------------------------
  observeEvent(input$button_to_g2_q2_b, {
    if (isTruthy(input$choose_g2_q2_a)) {
      updateTabsetPanel(session, "barlist_g2_q2", selected = "Group 2 Question 2-B")
    }
  })
  
  observeEvent(input$button_to_g2_q2_c, {
    if (isTruthy(input$choose_g2_q2_b)) {
      
      if (input$choose_g2_q2_a == input$choose_g2_q2_b) {
        hideTab("barlist_g2_q2", target = "Group 2 Question 2-C1")
        showTab("barlist_g2_q2", target = "Group 2 Question 2-C2")
        updateTabsetPanel(session, "barlist_g2_q2", selected = "Group 2 Question 2-C2")
      } else {
        updateTabsetPanel(session, "barlist_g2_q2", selected = "Group 2 Question 2-C1")
      }
    }
  })
  
  observeEvent(input$button_c1_to_g2_q3, {
    if (isTruthy(input$g2_q2_c1)) {
      updateTabsetPanel(session, "barlist", selected = "Group 2 Question 3")
      hideTab("barlist_g2_q3", target = "Group 2 Question 3-C2")
    }
  })
  
  observeEvent(input$button_c2_to_g2_q3, {
    if (isTruthy(input$choose_g2_q2_c2) & isTruthy(input$g2_q2_c2)) {
      updateTabsetPanel(session, "barlist", selected = "Group 2 Question 3")
      hideTab("barlist_g2_q3", target = "Group 2 Question 3-C2")
    }
  })
  
  # Buttons: Group 2 Question 3 ---------------------------------------
  observeEvent(input$button_to_g2_q3_b, {
    if (isTruthy(input$choose_g2_q3_a)) {
      updateTabsetPanel(session, "barlist_g2_q3", selected = "Group 2 Question 3-B")
    }
  })
  
  observeEvent(input$button_to_g2_q3_c, {
    if (isTruthy(input$choose_g2_q3_b)) {
      
      if (input$choose_g2_q3_a == input$choose_g2_q3_b) {
        hideTab("barlist_g2_q3", target = "Group 2 Question 3-C1")
        showTab("barlist_g2_q3", target = "Group 2 Question 3-C2")
        updateTabsetPanel(session, "barlist_g2_q3", selected = "Group 2 Question 3-C2")
      } else {
        updateTabsetPanel(session, "barlist_g2_q3", selected = "Group 2 Question 3-C1")
      }
    }
  })
  
  observeEvent(input$button_c1_to_g2_q4, {
    if (isTruthy(input$g2_q3_c1)) {
      updateTabsetPanel(session, "barlist", selected = "Group 2 Question 4")
      hideTab("barlist_g2_q4", target = "Group 2 Question 4-C2")
    }
  })
  
  observeEvent(input$button_c2_to_g2_q4, {
    if (isTruthy(input$choose_g2_q3_c2) & isTruthy(input$g2_q3_c2)) {
      updateTabsetPanel(session, "barlist", selected = "Group 2 Question 4")
      hideTab("barlist_g2_q4", target = "Group 2 Question 4-C2")
    }
  })
  
  # Buttons: Group 2 Question 4 ---------------------------------------
  observeEvent(input$button_to_g2_q4_b, {
    if (isTruthy(input$choose_g2_q4_a)) {
      updateTabsetPanel(session, "barlist_g2_q4", selected = "Group 2 Question 4-B")
    }
  })
  
  observeEvent(input$button_to_g2_q4_c, {
    if (isTruthy(input$choose_g2_q4_b)) {
      
      if (input$choose_g2_q4_a == input$choose_g2_q4_b) {
        hideTab("barlist_g2_q4", target = "Group 2 Question 4-C1")
        showTab("barlist_g2_q4", target = "Group 2 Question 4-C2")
        updateTabsetPanel(session, "barlist_g2_q4", selected = "Group 2 Question 4-C2")
      } else {
        updateTabsetPanel(session, "barlist_g2_q4", selected = "Group 2 Question 4-C1")
      }
    } 
  })
  
  observeEvent(input$button_g2_c1_to_background_info, {
    # if (isTruthy(input$g2_q4_c1)) {
    updateTabsetPanel(session, "barlist", selected = "Background Information")
    # }
  })
  
  observeEvent(input$button_g2_c2_to_background_info, {
    if (isTruthy(input$choose_g2_q4_c2) & isTruthy(input$g2_q4_c2)) {
      updateTabsetPanel(session, "barlist", selected = "Background Information")
    }
  })
  
  # Buttons: Group 3 Question 1 ---------------------------------------
  observeEvent(input$button_to_g3_q1_b, {
    if (isTruthy(input$choose_g3_q1_a)) {
      updateTabsetPanel(session, "barlist_g3_q1", selected = "Group 3 Question 1-B")
    }
  })
  
  observeEvent(input$button_to_g3_q1_c, {
    if (isTruthy(input$choose_g3_q1_b)) {
      
      if (input$choose_g3_q1_a == input$choose_g3_q1_b) {
        hideTab("barlist_g3_q1", target = "Group 3 Question 1-C1")
        showTab("barlist_g3_q1", target = "Group 3 Question 1-C2")
        updateTabsetPanel(session, "barlist_g3_q1", selected = "Group 3 Question 1-C2")
      } else {
        updateTabsetPanel(session, "barlist_g3_q1", selected = "Group 3 Question 1-C1")
      }
    }
  })
  
  observeEvent(input$button_c1_to_g3_q2, {
    if (isTruthy(input$g3_q1_c1)) {
      updateTabsetPanel(session, "barlist", selected = "Group 3 Question 2")
      hideTab("barlist_g3_q2", target = "Group 3 Question 2-C2")
    }
  })
  
  observeEvent(input$button_c2_to_g3_q2, {
    if (isTruthy(input$choose_g3_q1_c2) & isTruthy(input$g3_q1_c2)) {
      updateTabsetPanel(session, "barlist", selected = "Group 3 Question 2")
      hideTab("barlist_g3_q2", target = "Group 3 Question 2-C2")
    }
  })
  
  
  # Buttons: Group 3 Question 2 ---------------------------------------
  observeEvent(input$button_to_g3_q2_b, {
    if (isTruthy(input$choose_g3_q2_a)) {
      updateTabsetPanel(session, "barlist_g3_q2", selected = "Group 3 Question 2-B")
    }
  })
  
  observeEvent(input$button_to_g3_q2_c, {
    if (isTruthy(input$choose_g3_q2_b)) {
      
      if (input$choose_g3_q2_a == input$choose_g3_q2_b) {
        hideTab("barlist_g3_q2", target = "Group 3 Question 2-C1")
        showTab("barlist_g3_q2", target = "Group 3 Question 2-C2")
        updateTabsetPanel(session, "barlist_g3_q2", selected = "Group 3 Question 2-C2")
      } else {
        updateTabsetPanel(session, "barlist_g3_q2", selected = "Group 3 Question 2-C1")
      }
    } 
  })
  
  observeEvent(input$button_c1_to_g3_q3, {
    if (isTruthy(input$g3_q2_c1)) {
      updateTabsetPanel(session, "barlist", selected = "Group 3 Question 3")
      hideTab("barlist_g3_q3", target = "Group 3 Question 3-C2")
    }
  })
  
  observeEvent(input$button_c2_to_g3_q3, {
    if (isTruthy(input$choose_g3_q2_c2) & isTruthy(input$g3_q2_c2)) {
      updateTabsetPanel(session, "barlist", selected = "Group 3 Question 3")
      hideTab("barlist_g3_q3", target = "Group 3 Question 3-C2")
    }
  })
  # Buttons: Group 3 Question 3 ---------------------------------------
  observeEvent(input$button_to_g3_q3_b, {
    if (isTruthy(input$choose_g3_q3_a)) {
      updateTabsetPanel(session, "barlist_g3_q3", selected = "Group 3 Question 3-B")
    }
  })
  
  observeEvent(input$button_to_g3_q3_c, {
    if (isTruthy(input$choose_g3_q3_b)) {
      
      if (input$choose_g3_q3_a == input$choose_g3_q3_b) {
        hideTab("barlist_g3_q3", target = "Group 3 Question 3-C1")
        showTab("barlist_g3_q3", target = "Group 3 Question 3-C2")
        updateTabsetPanel(session, "barlist_g3_q3", selected = "Group 3 Question 3-C2")
      } else {
        updateTabsetPanel(session, "barlist_g3_q3", selected = "Group 3 Question 3-C1")
      }
    } 
  })
  
  observeEvent(input$button_c1_to_g3_q4, {
    if (isTruthy(input$g3_q3_c1)) {
      updateTabsetPanel(session, "barlist", selected = "Group 3 Question 4")
      hideTab("barlist_g3_q4", target = "Group 3 Question 4-C2")
    }
  })
  
  observeEvent(input$button_c2_to_g3_q4, {
    if (isTruthy(input$choose_g3_q3_c2) & isTruthy(input$g3_q3_c2)) {
      updateTabsetPanel(session, "barlist", selected = "Group 3 Question 4")
      hideTab("barlist_g3_q4", target = "Group 3 Question 4-C2")
    }
  })
  
  # Buttons: Group 3 Question 4 ---------------------------------------
  observeEvent(input$button_to_g3_q4_b, {
    if (isTruthy(input$choose_g3_q4_a)) {
      updateTabsetPanel(session, "barlist_g3_q4", selected = "Group 3 Question 4-B")
    }
  })
  
  observeEvent(input$button_to_g3_q4_c, {
    if (isTruthy(input$choose_g3_q4_b)) {
      
      if (input$choose_g3_q4_a == input$choose_g3_q4_b) {
        hideTab("barlist_g3_q4", target = "Group 3 Question 4-C1")
        showTab("barlist_g3_q4", target = "Group 3 Question 4-C2")
        updateTabsetPanel(session, "barlist_g3_q4", selected = "Group 3 Question 4-C2")
      } else {
        updateTabsetPanel(session, "barlist_g3_q4", selected = "Group 3 Question 4-C1")
      }
    } 
  })
  
  observeEvent(input$button_g3_c1_to_background_info, {
    if (isTruthy(input$g3_q4_c1)) {
      updateTabsetPanel(session, "barlist", selected = "Background Information")
    }
  })
  
  observeEvent(input$button_g3_c2_to_background_info, {
    if (isTruthy(input$choose_g3_q4_c2) & isTruthy(input$g3_q4_c2)) {
      updateTabsetPanel(session, "barlist", selected = "Background Information")
    }
  })
  # Buttons: Group 4 Question 1 ---------------------------------------
  observeEvent(input$button_to_g4_q1_b, {
    if (isTruthy(input$choose_g4_q1_a)) {
      updateTabsetPanel(session, "barlist_g4_q1", selected = "Group 4 Question 1-B")
    }
  })
  
  observeEvent(input$button_to_g4_q1_c, {
    if (isTruthy(input$choose_g4_q1_b)) {
      
      if (input$choose_g4_q1_a == input$choose_g4_q1_b) {
        hideTab("barlist_g4_q1", target = "Group 4 Question 1-C1")
        showTab("barlist_g4_q1", target = "Group 4 Question 1-C2")
        updateTabsetPanel(session, "barlist_g4_q1", selected = "Group 4 Question 1-C2")
      } else {
        updateTabsetPanel(session, "barlist_g4_q1", selected = "Group 4 Question 1-C1")
      }
    }
  })
  
  observeEvent(input$button_c1_to_g4_q2, {
    if (isTruthy(input$g4_q1_c1)) {
      updateTabsetPanel(session, "barlist", selected = "Group 4 Question 2")
      hideTab("barlist_g4_q2", target = "Group 4 Question 2-C2")
    }
  })
  
  observeEvent(input$button_c2_to_g4_q2, {
    if (isTruthy(input$choose_g4_q1_c2) & isTruthy(input$g4_q1_c2)) {
      updateTabsetPanel(session, "barlist", selected = "Group 4 Question 2")
      hideTab("barlist_g4_q2", target = "Group 4 Question 2-C2")
    }
  })
  
  # Buttons: Group 4 Question 2 ---------------------------------------
  observeEvent(input$button_to_g4_q2_b, {
    if (isTruthy(input$choose_g4_q2_a)) {
      updateTabsetPanel(session, "barlist_g4_q2", selected = "Group 4 Question 2-B")
    }
  })
  
  observeEvent(input$button_to_g4_q2_c, {
    if (isTruthy(input$choose_g4_q2_b)) {
      
      if (input$choose_g4_q2_a == input$choose_g4_q2_b) {
        hideTab("barlist_g4_q2", target = "Group 4 Question 2-C1")
        showTab("barlist_g4_q2", target = "Group 4 Question 2-C2")
        updateTabsetPanel(session, "barlist_g4_q2", selected = "Group 4 Question 2-C2")
      } else {
        updateTabsetPanel(session, "barlist_g4_q2", selected = "Group 4 Question 2-C1")
      }
    } 
  })
  
  observeEvent(input$button_c1_to_g4_q3, {
    if (isTruthy(input$g4_q2_c1)) {
      updateTabsetPanel(session, "barlist", selected = "Group 4 Question 3")
      hideTab("barlist_g4_q3", target = "Group 4 Question 3-C2")
    }
  })
  
  observeEvent(input$button_c2_to_g4_q3, {
    if (isTruthy(input$choose_g4_q2_c2) & isTruthy(input$g4_q2_c2)) {
      updateTabsetPanel(session, "barlist", selected = "Group 4 Question 3")
      hideTab("barlist_g4_q3", target = "Group 4 Question 3-C2")
    }
  })
  
  # Buttons: Group 4 Question 3 ---------------------------------------
  observeEvent(input$button_to_g4_q3_b, {
    if (isTruthy(input$choose_g4_q3_a)) {
      updateTabsetPanel(session, "barlist_g4_q3", selected = "Group 4 Question 3-B")
    }
  })
  
  observeEvent(input$button_to_g4_q3_c, {
    if (isTruthy(input$choose_g4_q3_b)) {
      
      if (input$choose_g4_q3_a == input$choose_g4_q3_b) {
        hideTab("barlist_g4_q3", target = "Group 4 Question 3-C1")
        showTab("barlist_g4_q3", target = "Group 4 Question 3-C2")
        updateTabsetPanel(session, "barlist_g4_q3", selected = "Group 4 Question 3-C2")
      } else {
        updateTabsetPanel(session, "barlist_g4_q3", selected = "Group 4 Question 3-C1")
      }
    } 
  })
  
  observeEvent(input$button_c1_to_g4_q4, {
    if (isTruthy(input$g4_q3_c1)) {
      updateTabsetPanel(session, "barlist", selected = "Group 4 Question 4")
      hideTab("barlist_g4_q4", target = "Group 4 Question 4-C2")
    }
  })
  
  observeEvent(input$button_c2_to_g4_q4, {
    if (isTruthy(input$choose_g4_q3_c2) & isTruthy(input$g4_q3_c2)) {
      updateTabsetPanel(session, "barlist", selected = "Group 4 Question 4")
      hideTab("barlist_g4_q4", target = "Group 4 Question 4-C2")
    }
  })
  
  # Buttons: Group 4 Question 4 ---------------------------------------
  observeEvent(input$button_to_g4_q4_b, {
    if (isTruthy(input$choose_g4_q4_a)) {
      updateTabsetPanel(session, "barlist_g4_q4", selected = "Group 4 Question 4-B")
    }
  })
  
  observeEvent(input$button_to_g4_q4_c, {
    if (isTruthy(input$choose_g4_q4_b)) {
      
      if (input$choose_g4_q4_a == input$choose_g4_q4_b) {
        hideTab("barlist_g4_q4", target = "Group 4 Question 4-C1")
        showTab("barlist_g4_q4", target = "Group 4 Question 4-C2")
        updateTabsetPanel(session, "barlist_g4_q4", selected = "Group 4 Question 4-C2")
      } else {
        updateTabsetPanel(session, "barlist_g4_q4", selected = "Group 4 Question 4-C1")
      }
    } 
  })
  
  observeEvent(input$button_g4_c1_to_background_info, {
    if (isTruthy(input$g4_q4_c1)) {
      updateTabsetPanel(session, "barlist", selected = "Background Information")
    }
  })
  
  observeEvent(input$button_g4_c2_to_background_info, {
    if (isTruthy(input$choose_g4_q4_c2) & isTruthy(input$g4_q4_c2)) {
      updateTabsetPanel(session, "barlist", selected = "Background Information")
    }
  })
  # Buttons: background info ---------------------------------------
  observeEvent(input$button_to_last_page, {
    if (isTruthy(input$choose_add_q1) & 
        isTruthy(input$choose_add_q2) & 
        isTruthy(input$choose_add_q3) & 
        isTruthy(input$choose_add_q4) & 
        isTruthy(input$choose_add_q6) & 
        isTruthy(input$choose_add_q7) & 
        isTruthy(input$choose_add_q8) & 
        isTruthy(input$choose_add_q9) & 
        isTruthy(input$choose_add_q10) & 
        isTruthy(input$choose_add_q11) & 
        isTruthy(input$choose_add_q12) & 
        isTruthy(input$choose_add_q13) &  
        isTruthy(input$choose_add_q15) & 
        isTruthy(input$choose_add_q16) & 
        isTruthy(input$choose_add_q17) & 
        isTruthy(input$choose_add_q18) & 
        isTruthy(input$choose_add_q19) & 
        isTruthy(input$choose_add_q20) & 
        isTruthy(input$choose_add_q21) & 
        isTruthy(input$choose_add_q22) & 
        isTruthy(input$choose_add_q23a) & 
        isTruthy(input$choose_add_q23b) & 
        isTruthy(input$choose_add_q23c) & 
        isTruthy(input$choose_add_q23d) & 
        isTruthy(input$choose_add_q23e) & 
        isTruthy(input$choose_add_q23f) & 
        isTruthy(input$choose_add_q23g) & 
        isTruthy(input$choose_add_q23h) & 
        isTruthy(input$choose_add_q23i) & 
        isTruthy(input$choose_add_q23j) & 
        isTruthy(input$choose_add_q23k)){
      updateTabsetPanel(session, "barlist", selected = "Thank You")
    }
  })
  
  
  # Table: table_background ---------------------------------------
  df_background <- data.frame(
    fund_no = c("Fund A", "Fund B", "Fund C"),
    type_fund = c("Target Date Fund", "Target Date Fund", "Target Date Fund"),
    target_date = as.integer(c(2060, 2060, 2060)),
    gross_expratio = percent(c(0.002, 0.002, 0.002), accuracy = 0.01),
    current_e = percent(c(0.935, 1, 0.8), accuracy = 0.01),
    current_fi = percent(c(0.065, 0, 0.2), accuracy = 0.1),
    middle_e = percent(c(0.7, 0.8, 0.5), accuracy = 0.1),
    middle_fi = percent(c(0.3, 0.2, 0.5), accuracy = 0.1),
    final_e = percent(c(0.5, 0.6, 0.4), accuracy = 0.1),
    final_fi = percent(c(0.5, 0.4, 0.6), accuracy = 0.1)
  )
  
  output$table_background <- renderTable({
    head(df_background)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation 
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income"
  ), 
  align = 'c')
  
  # Table: table_practice_q1 ---------------------------------------
  df_practice_q1 <- data.frame(
    fund_no = c("Fund A", "Fund B", "Fund C"),
    type_fund = c("Target Date Fund", "Target Date Fund", "Target Date Fund"),
    target_date = as.integer(c(2060, 2060, 2060)),
    gross_expratio = percent(c(0.002, 0.002, 0.002), accuracy = 0.01),
    current_e = percent(c(0.94, 1, 0.8), accuracy = 1),
    current_fi = percent(c(0.06, 0, 0.2), accuracy = 1),
    middle_e = percent(c(0.7, 0.8, 0.5), accuracy = 1),
    middle_fi = percent(c(0.3, 0.2, 0.5), accuracy = 1),
    final_e = percent(c(0.5, 0.6, 0.4), accuracy = 1),
    final_fi = percent(c(0.5, 0.4, 0.6), accuracy = 1)
  )
  
  output$table_practice_q1 <- renderTable({
    head(df_practice_q1)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation 
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income"
  ), 
  align = 'c')
  
  # Table: table_practice_q2 ---------------------------------------
  df_practice_q2 <- data.frame(
    fund_no = c("Fund A", "Fund B", "Fund C"),
    type_fund = c("Target Date Fund", "Target Date Fund", "Target Date Fund"),
    target_date = as.integer(c(2040, 2040, 2040)),
    gross_expratio = percent(c(0.002, 0.002, 0.002), accuracy = 0.01),
    current_e = percent(c(0.95, 0.8, 0.6), accuracy = 1),
    current_fi = percent(c(0.05, 0.2, 0.4), accuracy = 1),
    middle_e = percent(c(0.7, 0.5, 0.5), accuracy = 1),
    middle_fi = percent(c(0.3, 0.5, 0.5), accuracy = 1),
    final_e = percent(c(0.5, 0.3, 0.4), accuracy = 1),
    final_fi = percent(c(0.5, 0.7, 0.6), accuracy = 1)
  ) # CHECK THE ALLOCATIONS FOR FUND C
  
  output$table_practice_q2 <- renderTable({
    head(df_practice_q2)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation 
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income"
  ), 
  align = 'c')
  
  # Table: table_practice_q3 (floor & buffer) ---------------------------------------
  df_practice_q3_floor <- data.frame(
    return_actual = percent(c(-0.15, -0.05, 0.05, 0.15), accuracy = 1),
    return_rila = percent(c(-0.1, -0.05, 0.05, 0.08), accuracy = 1),
    reason = c("Credited return cannot be less than the -10% floor.",
               "",
               "",
               "Credited return cannot be more than the 8% cap.")
  )
  
  output$table_practice_q3_floor <- renderTable({
    head(df_practice_q3_floor)
  },
  rownames = FALSE,
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                                 .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                                 </style>
                                 <thead>
                                 <tr><th class='tg-c3ow'> Actual Return on Associated Index <th class='tg-c3ow'> Return Credited to RILA Account <th class='tg-c3ow'> Reasoning"
  ),
  align = c("ccl"))
  
  output$table_practice_q3_floor1 <- renderTable({
    head(df_practice_q3_floor)
  },
  rownames = FALSE,
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                                 .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                                 </style>
                                 <thead>
                                 <tr><th class='tg-c3ow'> Actual Return on Associated Index <th class='tg-c3ow'> Return Credited to RILA Account <th class='tg-c3ow'> Reasoning"
  ),
  align = c("ccl"))
  
  df_practice_q3_buffer <- data.frame(
    return_actual = percent(c(-0.15, -0.05, 0.05, 0.15), accuracy = 1),
    return_rila = percent(c(-0.05, 0, 0.05, 0.08), accuracy = 1),
    reason = c("Credited loss equals actual loss minus 10% buffer.", 
               "Buffer reduces small losses to 0.", 
               "",
               "Credited return cannot be more than the 8% cap.")
  )
  
  output$table_practice_q3_buffer <- renderTable({
    head(df_practice_q3_buffer)
  },
  rownames = FALSE,
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-c3ow'> Actual Return on Associated Index <th class='tg-c3ow'> Return Credited to RILA Account <th class='tg-c3ow'> Reasoning"
  ),
  align = c("ccl"))
  
  output$table_practice_q3_buffer1 <- renderTable({
    head(df_practice_q3_buffer)
  },
  rownames = FALSE,
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-c3ow'> Actual Return on Associated Index <th class='tg-c3ow'> Return Credited to RILA Account <th class='tg-c3ow'> Reasoning"
  ),
  align = c("ccl"))
  
  
  # Table: table_practice_q4 ---------------------------------------
  df_practice_q4 <- data.frame(
    fund_no = c("Fund A", "Fund B", "Fund C"),
    type_fund = c("Target Date RILA", "Target Date RILA", "Target Date RILA"),
    target_date = as.integer(c(2050, 2050, 2050)),
    gross_expratio = percent(c(0.002, 0.002, 0.002), accuracy = 0.01),
    current_floor = percent(c(-0.2437, -0.2087, -0.1887), accuracy = 0.01),
    current_cap = percent(c(0.2671, 0.2344, 0.2238), accuracy = 0.01),
    middle_floor = percent(c(-0.1487, -0.1137, -0.0937), accuracy = 0.01),
    middle_cap = percent(c(0.1902, 0.1601, 0.1421), accuracy = 0.01),
    final_floor = percent(c(-0.06, -0.025, -0.005), accuracy = 0.01),
    final_cap = percent(c(0.1119, 0.0793, 0.0606), accuracy = 0.01)
  )
  
  output$table_practice_q4 <- renderTable({
    head(df_practice_q4)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Table: table_practice_q5 ---------------------------------------
  df_practice_q5 <- data.frame(
    fund_no = c("Fund A", "Fund B", "Fund C"),
    type_fund = c("Target Date RILA", "Target Date RILA", "Target Date RILA"),
    target_date = as.integer(c(2050, 2050, 2050)),
    gross_expratio = percent(c(0.002, 0.002, 0.002), accuracy = 0.01),
    current_buffer = percent(c(0.0033, 0.0283, 0.0533), accuracy = 0.01),
    current_cap = percent(c(0.3382, 0.2218, 0.1729), accuracy = 0.01),
    middle_buffer = percent(c(0.0533, 0.0783, 0.1033), accuracy = 0.01),
    middle_cap = percent(c(0.1729, 0.1419, 0.1209), accuracy = 0.01),
    final_buffer = percent(c(0.1, 0.125, 0.15), accuracy = 0.01),
    final_cap = percent(c(0.1234, 0.1075, 0.0951), accuracy = 0.01)
  )
  
  output$table_practice_q5 <- renderTable({
    head(df_practice_q5)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  
  
  
  
  
  
  # Specs -------------------------------------------------------------------
  
  
  get_first_1 <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      cols <- 1:3
      
      vbins <- vbins_1()
      
      type = case_when(cs$con_type_vec[cols[1]] == "TDF" ~ "Target Date Fund",
                       cs$con_type_vec[cols[1]] == "Buffer" ~ "Target Date RILA",
                       cs$con_type_vec[cols[1]] == "Floor" ~ "Target Date RILA")
      
      if(type != "Target Date Fund"){
        first <- as.numeric(cs$con_spec_vec[start_year, cols ])
        middle <- as.numeric(cs$con_spec_vec[round((50 - start_year)/2), cols ])
        end <- as.numeric(cs$con_spec_vec[50, cols])
        
        first_l <- as.numeric(cs$con_cap_vec[start_year, 1, cols])
        middle_l <- as.numeric(cs$con_cap_vec[round((50 - start_year)/2), 1, cols])
        end_l <- as.numeric(cs$con_cap_vec[50, 1, cols])
      } else {
        first <- as.numeric(cs$con_spec_vec[start_year, cols ])
        middle <- as.numeric(cs$con_spec_vec[round((50 - start_year)/2), cols ])
        end <- as.numeric(cs$con_spec_vec[50, cols])
        
        first_l <- 1 - first
        middle_l <- 1 - middle
        end_l <- 1 - end
      }
      
      retirebenefit_worst <-  c(vbins[cols[1], 1], 
                                vbins[cols[2], 2], 
                                vbins[cols[3], 2])
      retirebenefit_expected <- c(mean(vbins[cols[1], 2:11]), 
                                  mean(vbins[cols[2], 2:11]), 
                                  mean(vbins[cols[3], 2:11]))
      retirebenefit_best <-  c(vbins[cols[1], 12], 
                               vbins[cols[2], 11], 
                               vbins[cols[3], 11])
      retirebenefit_sd <- c(sd(vbins[cols[1], 2:11]), 
                            sd(vbins[cols[2], 2:11]), 
                            sd(vbins[cols[3], 2:11]))
      
      
      
      
      
      specs <- list(type = type,
                    f = first, 
                    m = middle, 
                    l = end,
                    fb = first_l, 
                    mb = middle_l, 
                    lb = end_l,
                    rbw = retirebenefit_worst,
                    rbe = retirebenefit_expected,
                    rbb = retirebenefit_best,
                    rbs = retirebenefit_sd)
      return(specs)
    }
  )
  
  get_first_2 <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      cols <- 7:9
      
      
      vbins <- vbins_1()
      
      type = case_when(cs$con_type_vec[cols[1]] == "TDF" ~ "Target Date Fund",
                       cs$con_type_vec[cols[1]] == "Buffer" ~ "Target Date RILA",
                       cs$con_type_vec[cols[1]] == "Floor" ~ "Target Date RILA")
      
      if(type != "Target Date Fund"){
        first <- -as.numeric(cs$con_spec_vec[start_year, cols ])
        middle <- -as.numeric(cs$con_spec_vec[round((50 - start_year)/2), cols ])
        end <- -as.numeric(cs$con_spec_vec[50, cols])
        
        first_l <- as.numeric(cs$con_cap_vec[start_year, 1, cols])
        middle_l <- as.numeric(cs$con_cap_vec[round((50 - start_year)/2), 1, cols])
        end_l <- as.numeric(cs$con_cap_vec[50, 1, cols])
      } else {
        first <- as.numeric(cs$con_spec_vec[start_year, cols ])
        middle <- as.numeric(cs$con_spec_vec[round((50 - start_year)/2), cols ])
        end <- as.numeric(cs$con_spec_vec[50, cols])
        
        first_l <- 1 - first
        middle_l <- 1 - middle
        end_l <- 1 - end
      }
      retirebenefit_worst <-  c(vbins[cols[1], 1], vbins[cols[2], 2], vbins[cols[3], 2])
      retirebenefit_expected <- c(mean(vbins[cols[1], 2:11]), mean(vbins[cols[2], 2:11]), mean(vbins[cols[3], 2:11]))
      retirebenefit_best <-  c(vbins[cols[1], 12], vbins[cols[2], 11], vbins[cols[3], 11])
      retirebenefit_sd <- c(sd(vbins[cols[1], 2:11]), sd(vbins[cols[2], 2:11]), sd(vbins[cols[3], 2:11]))
      
      
      specs <- list(type = type,
                    f = first, 
                    m = middle, 
                    l = end,
                    fb = first_l, 
                    mb = middle_l, 
                    lb = end_l,
                    rbw = retirebenefit_worst,
                    rbe = retirebenefit_expected,
                    rbb = retirebenefit_best,
                    rbs = retirebenefit_sd)
      return(specs)
    }
  )
  
  get_first_3 <- reactive(
    {
      start_year <- max(year_retire() - 2022 - 49, 1)
      cols <- 4:6
      
      
      vbins <- vbins_1()
      
      type = case_when(cs$con_type_vec[cols[1]] == "TDF" ~ "Target Date Fund",
                       cs$con_type_vec[cols[1]] == "Buffer" ~ "Target Date RILA",
                       cs$con_type_vec[cols[1]] == "Floor" ~ "Target Date RILA")
      
      if(type != "Target Date Fund"){
        first <- as.numeric(cs$con_spec_vec[start_year, cols ])
        middle <- as.numeric(cs$con_spec_vec[round((50 - start_year)/2), cols ])
        end <- as.numeric(cs$con_spec_vec[50, cols])
        
        first_l <- as.numeric(cs$con_cap_vec[start_year, 1, cols])
        middle_l <- as.numeric(cs$con_cap_vec[round((50 - start_year)/2), 1, cols])
        end_l <- as.numeric(cs$con_cap_vec[50, 1, cols])
      } else {
        first <- as.numeric(cs$con_spec_vec[start_year, cols ])
        middle <- as.numeric(cs$con_spec_vec[round((50 - start_year)/2), cols ])
        end <- as.numeric(cs$con_spec_vec[50, cols])
        
        first_l <- 1 - first
        middle_l <- 1 - middle
        end_l <- 1 - end
      }
      retirebenefit_worst <-  c(vbins[cols[1], 1], vbins[cols[2], 2], vbins[cols[3], 2])
      retirebenefit_expected <- c(mean(vbins[cols[1], 2:11]), mean(vbins[cols[2], 2:11]), mean(vbins[cols[3], 2:11]))
      retirebenefit_best <-  c(vbins[cols[1], 12], vbins[cols[2], 11], vbins[cols[3], 11])
      retirebenefit_sd <- c(sd(vbins[cols[1], 2:11]), sd(vbins[cols[2], 2:11]), sd(vbins[cols[3], 2:11]))
      
      
      specs <- list(type = type,
                    f = first, 
                    m = middle, 
                    l = end,
                    fb = first_l, 
                    mb = middle_l, 
                    lb = end_l,
                    rbw = retirebenefit_worst,
                    rbe = retirebenefit_expected,
                    rbb = retirebenefit_best,
                    rbs = retirebenefit_sd)
      return(specs)
    }
  )
  
  get_first_4a <- reactive(
    {
      
      start_year <- max(year_retire() - 2022, 1)
      
      if(group_assigned == 1){
        cols <- case_when(input$choose_g1_q1_a == "Fund A" ~ 1,
                          input$choose_g1_q1_a == "Fund B" ~ 2,
                          input$choose_g1_q1_a == "Fund C" ~ 3)
      }
      
      if(group_assigned == 2){
        cols <- case_when(input$choose_g2_q1_a == "Fund A" ~ 1,
                          input$choose_g2_q1_a == "Fund B" ~ 2,
                          input$choose_g2_q1_a == "Fund C" ~ 3)
      }
      
      if(group_assigned == 3){
        cols <- case_when(input$choose_g3_q1_a == "Fund A" ~ 1,
                          input$choose_g3_q1_a == "Fund B" ~ 2,
                          input$choose_g3_q1_a == "Fund C" ~ 3)
      }
      
      if(group_assigned == 4){
        cols <- case_when(input$choose_g4_q1_a == "Fund A" ~ 1,
                          input$choose_g4_q1_a == "Fund B" ~ 2,
                          input$choose_g4_q1_a == "Fund C" ~ 3)
      }
      
      vbins <- vbins_1()
      # cols <- 1
      
      type = case_when(cs$con_type_vec[cols[1]] == "TDF" ~ "Target Date Fund",
                       cs$con_type_vec[cols[1]] == "Buffer" ~ "Target Date RILA",
                       cs$con_type_vec[cols[1]] == "Floor" ~ "Target Date RILA")
      
      if(type != "Target Date Fund"){
        first <- -as.numeric(cs$con_spec_vec[start_year, cols ])
        middle <- -as.numeric(cs$con_spec_vec[round((50 - start_year)/2), cols ])
        end <- -as.numeric(cs$con_spec_vec[50, cols])
        
        first_l <- as.numeric(cs$con_cap_vec[start_year, 1, cols])
        middle_l <- as.numeric(cs$con_cap_vec[round((50 - start_year)/2), 1, cols])
        end_l <- as.numeric(cs$con_cap_vec[50, 1, cols])
      } else {
        first <- as.numeric(cs$con_spec_vec[start_year, cols ])
        middle <- as.numeric(cs$con_spec_vec[round((50 - start_year)/2), cols ])
        end <- as.numeric(cs$con_spec_vec[50, cols])
        
        first_l <- 1 - first
        middle_l <- 1 - middle
        end_l <- 1 - end
      }
      
      retirebenefit_worst <-  c(vbins[cols[1], 1])
      retirebenefit_expected <- c(mean(vbins[cols[1], 2:11]))
      retirebenefit_best <-  c(vbins[cols[1], 12])
      retirebenefit_sd <- c(sd(vbins[cols[1], 2:11]))
      
      
      specs <- list(type = type,
                    f = first, 
                    m = middle, 
                    l = end,
                    fb = first_l, 
                    mb = middle_l, 
                    lb = end_l,
                    rbw = retirebenefit_worst,
                    rbe = retirebenefit_expected,
                    rbb = retirebenefit_best,
                    rbs = retirebenefit_sd)
      return(specs)
    }
  )
  
  
  
  get_first_4b <- reactive(
    {
      
      
      start_year <- max(year_retire() - 2022, 1)
      
      if(group_assigned == 1){
        cols <- case_when(input$choose_g1_q2_a == "Fund A" ~ 4,
                          input$choose_g1_q2_a == "Fund B" ~ 5,
                          input$choose_g1_q2_a == "Fund C" ~ 6)
      }
      
      if(group_assigned == 2){
        cols <- case_when(input$choose_g2_q2_a == "Fund A" ~ 4,
                          input$choose_g2_q2_a == "Fund B" ~ 5,
                          input$choose_g2_q2_a == "Fund C" ~ 6)
      }
      
      if(group_assigned == 3){
        cols <- case_when(input$choose_g3_q2_a == "Fund A" ~ 4,
                          input$choose_g3_q2_a == "Fund B" ~ 5,
                          input$choose_g3_q2_a == "Fund C" ~ 6)
      }
      
      if(group_assigned == 4){
        cols <- case_when(input$choose_g4_q2_a == "Fund A" ~ 4,
                          input$choose_g4_q2_a == "Fund B" ~ 5,
                          input$choose_g4_q2_a == "Fund C" ~ 6)
      }
      
      vbins <- vbins_1()
      
      type = case_when(cs$con_type_vec[cols[1]] == "TDF" ~ "Target Date Fund",
                       cs$con_type_vec[cols[1]] == "Buffer" ~ "Target Date RILA",
                       cs$con_type_vec[cols[1]] == "Floor" ~ "Target Date RILA")
      
      if(type != "Target Date Fund"){
        first <- -as.numeric(cs$con_spec_vec[start_year, cols ])
        middle <- -as.numeric(cs$con_spec_vec[round((50 - start_year)/2), cols ])
        end <- -as.numeric(cs$con_spec_vec[50, cols])
        
        first_l <- as.numeric(cs$con_cap_vec[start_year, 1, cols])
        middle_l <- as.numeric(cs$con_cap_vec[round((50 - start_year)/2), 1, cols])
        end_l <- as.numeric(cs$con_cap_vec[50, 1, cols])
      } else {
        first <- as.numeric(cs$con_spec_vec[start_year, cols ])
        middle <- as.numeric(cs$con_spec_vec[round((50 - start_year)/2), cols ])
        end <- as.numeric(cs$con_spec_vec[50, cols])
        
        first_l <- 1 - first
        middle_l <- 1 - middle
        end_l <- 1 - end
      }
      
      retirebenefit_worst <-  c(vbins[cols[1], 1])
      retirebenefit_expected <- c(mean(vbins[cols[1], 2:11]))
      retirebenefit_best <-  c(vbins[cols[1], 12])
      retirebenefit_sd <- c(sd(vbins[cols[1], 2:11]))
      
      specs <- list(type = type,
                    f = first, 
                    m = middle, 
                    l = end,
                    fb = first_l, 
                    mb = middle_l, 
                    lb = end_l,
                    rbw = retirebenefit_worst,
                    rbe = retirebenefit_expected,
                    rbb = retirebenefit_best,
                    rbs = retirebenefit_sd)
      return(specs)
    }
  )
  
  get_first_4c <- reactive(
    {
      
      start_year <- max(year_retire() - 2022, 1)
      
      if(group_assigned == 1){
        cols <- case_when(input$choose_g1_q3_a == "Fund A" ~ 7,
                          input$choose_g1_q3_a == "Fund B" ~ 8,
                          input$choose_g1_q3_a == "Fund C" ~ 9)
      }
      
      if(group_assigned == 2){
        cols <- case_when(input$choose_g2_q3_a == "Fund A" ~ 7,
                          input$choose_g2_q3_a == "Fund B" ~ 8,
                          input$choose_g2_q3_a == "Fund C" ~ 9)
      }
      
      if(group_assigned == 3){
        cols <- case_when(input$choose_g3_q3_a == "Fund A" ~ 7,
                          input$choose_g3_q3_a == "Fund B" ~ 8,
                          input$choose_g3_q3_a == "Fund C" ~ 9)
      }
      
      if(group_assigned == 4){
        cols <- case_when(input$choose_g4_q3_a == "Fund A" ~ 7,
                          input$choose_g4_q3_a == "Fund B" ~ 8,
                          input$choose_g4_q3_a == "Fund C" ~ 9)
      }
      
      vbins <- vbins_1()
      
      type = case_when(cs$con_type_vec[cols[1]] == "TDF" ~ "Target Date Fund",
                       cs$con_type_vec[cols[1]] == "Buffer" ~ "Target Date RILA",
                       cs$con_type_vec[cols[1]] == "Floor" ~ "Target Date RILA")
      
      if(type != "Target Date Fund"){
        first <- as.numeric(cs$con_spec_vec[start_year, cols ])
        middle <- as.numeric(cs$con_spec_vec[round((50 - start_year)/2), cols ])
        end <- as.numeric(cs$con_spec_vec[50, cols])
        
        first_l <- as.numeric(cs$con_cap_vec[start_year, 1, cols])
        middle_l <- as.numeric(cs$con_cap_vec[round((50 - start_year)/2), 1, cols])
        end_l <- as.numeric(cs$con_cap_vec[50, 1, cols])
        
      } else {
        first <- as.numeric(cs$con_spec_vec[start_year, cols ])
        middle <- as.numeric(cs$con_spec_vec[round((50 - start_year)/2), cols ])
        end <- as.numeric(cs$con_spec_vec[50, cols])
        
        first_l <- 1 - first
        middle_l <- 1 - middle
        end_l <- 1 - end
      }
      
      retirebenefit_worst <-  c(vbins[cols[1], 1])
      retirebenefit_expected <- c(mean(vbins[cols[1], 2:11]))
      retirebenefit_best <-  c(vbins[cols[1], 12])
      retirebenefit_sd <- c(sd(vbins[cols[1], 2:11]))
      
      specs <- list(type = type,
                    f = first, 
                    m = middle, 
                    l = end,
                    fb = first_l, 
                    mb = middle_l, 
                    lb = end_l,
                    rbw = retirebenefit_worst,
                    rbe = retirebenefit_expected,
                    rbb = retirebenefit_best,
                    rbs = retirebenefit_sd)
      return(specs)
    }
  )
  
  # Table: table_g1_q1_a ---------------------------------------
  
  df_g1_q1_a <- reactive(
    {
      specs <- get_first_1()
      data.frame(
        fund_no = c("Fund A", "Fund B", "Fund C"),
        type_fund = c("Target Date Fund", "Target Date Fund", "Target Date Fund"),
        target_date = as.integer(c(year_retire(), year_retire(), year_retire())),
        gross_expratio = percent(c(0.002, 0.002, 0.002), accuracy = 0.01),
        current_e = percent(c(specs$f[1], specs$f[2], specs$f[3]), accuracy = 1),
        current_fi = percent(c(specs$fb[1], specs$fb[2], specs$fb[3]), accuracy = 1),
        middle_e = percent(c(specs$m[1], specs$m[2], specs$m[3]), accuracy = 1),
        middle_fi = percent(c(specs$mb[1], specs$mb[2], specs$mb[3]), accuracy = 1),
        final_e = percent(c(specs$l[1], specs$l[2], specs$l[3]), accuracy = 1),
        final_fi = percent(c(specs$lb[1], specs$lb[2], specs$lb[3]), accuracy = 1)
      ) %>% return()
    }
  )
  
  
  output$table_g1_q1_a <- renderTable({
    head(df_g1_q1_a())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation 
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income"
  ), 
  align = 'c')
  
  # Table: table_g1_q1_b ---------------------------------------
  df_g1_q1_b <- reactive(
    {
      
      dat <- df_g1_q1_a()
      
      
      select_g1_q1_a <- switch(input$choose_g1_q1_a,
                               "Fund A" = 1,
                               "Fund B" = 2,
                               "Fund C" = 3)
      
      dat$gross_expratio[select_g1_q1_a] = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01) # Gross Expense Ratio changes for fund selected in Part A.
      return(dat)
    }
  )
  
  output$table_g1_q1_b <- renderTable({
    
    df_g1_q1_b() %>% head()
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation 
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income"
  ), 
  align = 'c')
  
  # Choices: table_g1_q1_c2 ---------------------------------------
  observeEvent(input$button_to_g1_q1_c, {
    if (isTruthy(input$choose_g1_q1_b)) {
      choices_g1_q1_c2 <- switch(input$choose_g1_q1_b,
                                 "Fund A" = c("Fund B", "Fund C"),
                                 "Fund B" = c("Fund A", "Fund C"),
                                 "Fund C" = c("Fund A", "Fund B"))
      updateRadioButtons(session, "choose_g1_q1_c2", choices = choices_g1_q1_c2, selected = character(0))
    }
  })
  
  # Table: table_g1_q2_a ---------------------------------------
  
  
  df_g1_q2_a <- reactive( 
    {
      specs <- get_first_2()
      data.frame(
        fund_no = c("Fund A", "Fund B", "Fund C"),
        type_fund = c(specs$type, specs$type, specs$type),
        target_date = as.integer(c(year_retire(), year_retire(), year_retire())),
        gross_expratio = percent(c(0.002, 0.002, 0.002), accuracy = 0.01),
        current_floor = percent(c(specs$f[1], specs$f[2], specs$f[3]), accuracy = .1),
        current_cap = percent(c(specs$fb[1], specs$fb[2], specs$fb[3]), accuracy = .1),
        middle_floor = percent(c(specs$m[1], specs$m[2], specs$m[3]), accuracy = .1),
        middle_cap = percent(c(specs$mb[1], specs$mb[2], specs$mb[3]), accuracy = .1),
        final_floor = percent(c(specs$l[1], specs$l[2], specs$l[3]), accuracy = .1),
        final_cap = percent(c(specs$lb[1], specs$lb[2], specs$lb[3]), accuracy = .1)
        
      )
    }
  )
  
  output$table_g1_q2_a <- renderTable({
    df_g1_q2_a() %>% head()
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Table: table_g1_q2_b ---------------------------------------
  
  
  
  df_g1_q2_b <- reactive(
    {
      dat <- df_g1_q2_a()
      
      select_g1_q2_a <- switch(input$choose_g1_q2_a,
                               "Fund A" = 1,
                               "Fund B" = 2,
                               "Fund C" = 3)
      
      dat$gross_expratio[select_g1_q2_a] <- percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01) # Gross Expense Ratio changes for fund selected in Part A.
      
      dat$current_cap[select_g1_q2_a] <- if (select_g1_q2_a == 1) {
        percent(update_current_cap_g1_q2_funda(), accuracy = .1) 
      } else if (select_g1_q2_a == 2) {
        percent(update_current_cap_g1_q2_fundb(), accuracy = .1)
      } else {
        percent(update_current_cap_g1_q2_fundc(), accuracy = .1)
      }
      
      dat$middle_cap[select_g1_q2_a] <- if (select_g1_q2_a == 1) {
        percent(update_middle_cap_g1_q2_funda(), accuracy = .1) 
      } else if (select_g1_q2_a == 2) {
        percent(update_middle_cap_g1_q2_fundb(), accuracy = .1)
      } else {
        percent(update_middle_cap_g1_q2_fundc(), accuracy = .1)
      }
      
      dat$final_cap[select_g1_q2_a] <-  if (select_g1_q2_a == 1) {
        percent(update_final_cap_g1_q2_funda(), accuracy = .1) 
      } else if (select_g1_q2_a == 2) {
        percent(update_final_cap_g1_q2_fundb(), accuracy = .1)
      } else {
        percent(update_final_cap_g1_q2_fundc(), accuracy = .1)
      } # Cap level updated based on fund selection in Part A and expense ratio.
      
      return(dat)
    }
  )
  
  output$table_g1_q2_b <- renderTable({
    df_g1_q2_b() %>% head()
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Choices: table_g1_q2_c2 ---------------------------------------
  observeEvent(input$button_to_g1_q2_c, {
    if (isTruthy(input$choose_g1_q2_b)) {
      choices_g1_q2_c2 <- switch(input$choose_g1_q2_b,
                                 "Fund A" = c("Fund B", "Fund C"),
                                 "Fund B" = c("Fund A", "Fund C"),
                                 "Fund C" = c("Fund A", "Fund B"))
      updateRadioButtons(session, "choose_g1_q2_c2", choices = choices_g1_q2_c2, selected = character(0))
    }
  })
  
  # Table: table_g1_q3_a ---------------------------------------
  
  
  df_g1_q3_a <- reactive( 
    {
      specs <- get_first_3()
      data.frame(
        fund_no = c("Fund A", "Fund B", "Fund C"),
        type_fund = c(specs$type, specs$type, specs$type),
        target_date = as.integer(c(year_retire(), year_retire(), year_retire())),
        gross_expratio = percent(c(0.002, 0.002, 0.002), accuracy = 0.01),
        current_floor = percent(c(specs$f[1], specs$f[2], specs$f[3]), accuracy = .1),
        current_cap = percent(c(specs$fb[1], specs$fb[2], specs$fb[3]), accuracy = .1),
        middle_floor = percent(c(specs$m[1], specs$m[2], specs$m[3]), accuracy = .1),
        middle_cap = percent(c(specs$mb[1], specs$mb[2], specs$mb[3]), accuracy = .1),
        final_floor = percent(c(specs$l[1], specs$l[2], specs$l[3]), accuracy = .1),
        final_cap = percent(c(specs$lb[1], specs$lb[2], specs$lb[3]), accuracy = .1)
        
      )
    }
  )
  
  output$table_g1_q3_a <- renderTable({
    df_g1_q3_a() %>% head()
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Table: table_g1_q3_b ---------------------------------------
  df_g1_q3_b <- reactive(
    {
      dat <- df_g1_q3_a()
      select_g1_q3_a <- switch(input$choose_g1_q3_a,
                               "Fund A" = 1,
                               "Fund B" = 2,
                               "Fund C" = 3)
      dat$gross_expratio[select_g1_q3_a] = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01) # Gross Expense Ratio changes for fund selected in Part A.
      
      dat$current_cap[select_g1_q3_a] = if (select_g1_q3_a == 1) {
        percent(update_current_cap_g1_q3_funda(), accuracy = 0.1) 
      } else if (select_g1_q3_a == 2) {
        percent(update_current_cap_g1_q3_fundb(), accuracy = 0.1)
      } else {
        percent(update_current_cap_g1_q3_fundc(), accuracy = 0.1)
      }
      
      dat$middle_cap[select_g1_q3_a] = if (select_g1_q3_a == 1) {
        percent(update_middle_cap_g1_q3_funda(), accuracy = 0.1) 
      } else if (select_g1_q3_a == 2) {
        percent(update_middle_cap_g1_q3_fundb(), accuracy = 0.1)
      } else {
        percent(update_middle_cap_g1_q3_fundc(), accuracy = 0.1)
      }
      
      dat$final_cap[select_g1_q3_a] = if (select_g1_q3_a == 1) {
        percent(update_final_cap_g1_q3_funda(), accuracy = 0.1) 
      } else if (select_g1_q3_a == 2) {
        percent(update_final_cap_g1_q3_fundb(), accuracy = 0.1)
      } else {
        percent(update_final_cap_g1_q3_fundc(), accuracy = 0.1)
      }# Cap level updated based on fund selection in Part A and expense ratio.
      
      return(dat)
    }
  )
  
  
  output$table_g1_q3_b <- renderTable({
    df_g1_q3_b() %>% head()
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Choices: table_g1_q3_c2 ---------------------------------------
  observeEvent(input$button_to_g1_q3_c, {
    if (isTruthy(input$choose_g1_q3_b)) {
      choices_g1_q3_c2 <- switch(input$choose_g1_q3_b,
                                 "Fund A" = c("Fund B", "Fund C"),
                                 "Fund B" = c("Fund A", "Fund C"),
                                 "Fund C" = c("Fund A", "Fund B"))
      updateRadioButtons(session, "choose_g1_q3_c2", choices = choices_g1_q3_c2, selected = character(0))
    }
  })
  
  # Table: table_g1_q4_a ---------------------------------------
  df_g1_q4_a1 <- reactive(
    {
      specs <- get_first_4a()
      data.frame(
        fund_no = c("Fund A"),
        type_fund = c(specs$type[1]),
        target_date = as.integer(c(year_retire())),
        gross_expratio = percent(c(0.002), accuracy = 0.01),
        current_e = percent(c(specs$f[1]), accuracy = 1),
        current_fi = percent(c(specs$fb[1]), accuracy = 1),
        middle_e = percent(c(specs$m[1]), accuracy = 1),
        middle_fi = percent(c(specs$mb[1]), accuracy = 1),
        final_e = percent(c(specs$l[1]), accuracy = 1),
        final_fi = percent(c(specs$lb[1]), accuracy = 1)
      )
    }
  )
  
  
  
  
  output$table_g1_q4_a1 <- renderTable({
    df_g1_q4_a1() %>% head()
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation 
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income"
  ), 
  align = 'c')
  
  
  df_g1_q4_a2 <- reactive(
    {
      specs <- get_first_4b()
      data.frame(
        fund_no = c("Fund B"),
        type_fund = c(specs$type[1]),
        target_date = as.integer(c(year_retire())),
        gross_expratio = percent(c(0.002), accuracy = 0.01),
        current_floor = percent(c(specs$f[1]), accuracy = .1),
        current_cap = percent(c(specs$fb[1]), accuracy = .1),
        middle_floor = percent(c(specs$m[1]), accuracy = .1),
        middle_cap = percent(c(specs$mb[1]), accuracy = .1),
        final_floor = percent(c(specs$l[1]), accuracy = .1),
        final_cap = percent(c(specs$lb[1]), accuracy = .1)
      )
    }
  )
  
  output$table_g1_q4_a2 <- renderTable({
    df_g1_q4_a2() %>% head()
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  
  df_g1_q4_a3 <- reactive(
    {
      specs <- get_first_4c()
      data.frame(
        fund_no = c("Fund C"),
        type_fund = c(specs$type[1]),
        target_date = as.integer(c(year_retire())),
        gross_expratio = percent(c(0.002), accuracy = 0.01),
        current_buffer = percent(c(specs$f[1]), accuracy = .1),
        current_cap = percent(c(specs$fb[1]), accuracy = .1),
        middle_buffer = percent(c(specs$m[1]), accuracy = .1),
        middle_cap = percent(c(specs$mb[1]), accuracy = .1),
        final_buffer = percent(c(specs$l[1]), accuracy = .1),
        final_cap = percent(c(specs$lb[1]), accuracy = .1)
      )
    }
  )
  
  output$table_g1_q4_a3 <- renderTable({
    df_g1_q4_a3() %>% head()
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Table: table_g1_q4_b ---------------------------------------
  output$table_g1_q4_b1 <- renderTable({
    dat1 <- df_g1_q4_a1()
    
    
    
    if (input$choose_g1_q4_a == "Fund A") {
      dat1$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01) # Gross Expense Ratio changes for fund selected in Part A.
    }
    head(dat1)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation 
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income"
  ), 
  align = 'c')
  
  output$table_g1_q4_b2 <- renderTable({
    dat2 <- df_g1_q4_a2()
    
    if (input$choose_g1_q4_a == "Fund B") {
      dat2$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01) # Gross Expense Ratio changes for fund selected in Part A.
      dat2$current_cap = percent(update_current_cap_g1_q2_funda(), accuracy = 0.1)
      dat2$middle_cap = percent(update_middle_cap_g1_q2_funda(), accuracy = 0.1)
      dat2$final_cap = percent(update_final_cap_g1_q2_funda(), accuracy = 0.1)# Cap level updated based on fund selection in Part A and expense ratio.
    }
    head(dat2)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  output$table_g1_q4_b3 <- renderTable({
    dat3 <- df_g1_q4_a3()
    
    if (input$choose_g1_q4_a == "Fund C") {
      dat3$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01) # Gross Expense Ratio changes for fund selected in Part A.
      dat3$current_cap = percent(update_current_cap_g1_q3_funda(), accuracy = 0.1)
      dat3$middle_cap = percent(update_middle_cap_g1_q3_funda(), accuracy = 0.1)
      dat3$final_cap = percent(update_final_cap_g1_q3_funda(), accuracy = 0.1) # Cap level updated based on fund selection in Part A and expense ratio.
    }
    head(dat3)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Choices: table_g1_q4_c2 ---------------------------------------
  observeEvent(input$button_to_g1_q4_c, {
    if (isTruthy(input$choose_g1_q4_b)) {
      choices_g1_q4_c2 <- switch(input$choose_g1_q4_b,
                                 "Fund A" = c("Fund B", "Fund C"),
                                 "Fund B" = c("Fund A", "Fund C"),
                                 "Fund C" = c("Fund A", "Fund B"))
      updateRadioButtons(session, "choose_g1_q4_c2", choices = choices_g1_q4_c2, selected = character(0))
    }
  })
  
  
  # Table: table_g2_q1_a ---------------------------------------
  
  
  
  
  
  df_g2_q1_a1 <- reactive(
    {
      specs <- get_first_1()
      data.frame(
        fund_no = c("Fund A"),
        type_fund = c("Target Date Fund"),
        target_date = as.integer(c(year_retire())),
        gross_expratio = percent(c(0.002), accuracy = 0.01),
        current_e = percent(specs$f[2], accuracy = 1),
        current_fi = percent(specs$fb[2], accuracy = 1),
        middle_e = percent(specs$m[2], accuracy = 1),
        middle_fi = percent(specs$mb[2], accuracy = 1),
        final_e = percent(specs$l[2], accuracy = 1),
        final_fi = percent(specs$lb[2], accuracy = 1)
      )
    }
  )
  
  output$table_g2_q1_a1 <- renderTable({
    
    head(df_g2_q1_a1())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation 
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income"
  ), 
  align = 'c')
  
  
  df_g2_q1_a2 <- reactive(
    {
      specs <- get_first_1()
      data.frame(
        fund_no = c("Fund B", "Fund C"),
        type_fund = c("Target Date Fund", "Target Date Fund"),
        target_date = as.integer(c(year_retire(), year_retire())),
        gross_expratio = percent(c(0.002, 0.002), accuracy = 0.01),
        current_e = percent(c(specs$f[1], specs$f[3]), accuracy = 1),
        current_fi = percent(c(specs$fb[1], specs$fb[3]), accuracy = 1),
        middle_e = percent(c(specs$m[1], specs$m[3]), accuracy = 1),
        middle_fi = percent(c(specs$mb[1], specs$mb[3]), accuracy = 1),
        final_e = percent(c(specs$l[1], specs$l[3]), accuracy = 1),
        final_fi = percent(c(specs$lb[1], specs$lb[3]), accuracy = 1)
      )
    }
  )
  
  output$table_g2_q1_a2 <- renderTable({
    
    head(df_g2_q1_a2())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation 
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income"
  ), 
  align = 'c')
  
  # Table: table_g2_q1_b ---------------------------------------
  output$table_g2_q1_b1 <- renderTable({
    dat <- df_g2_q1_a1()
    if (input$choose_g2_q1_a == "Fund A") {
      dat$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01) # Gross Expense Ratio changes for fund selected in Part A.
    }
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation 
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income"
  ), 
  align = 'c')
  
  output$table_g2_q1_b2 <- renderTable({
    
    dat <- df_g2_q1_a2()
    select_g2_q1_a <- switch(input$choose_g2_q1_a,
                             "Fund B" = 1,
                             "Fund C" = 2)
    dat$gross_expratio[select_g2_q1_a] = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01) # Gross Expense Ratio changes for fund selected in Part A.
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation 
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income"
  ), 
  align = 'c')
  
  # Choices: table_g2_q1_c2 ---------------------------------------
  observeEvent(input$button_to_g2_q1_c, {
    if (isTruthy(input$choose_g2_q1_b)) {
      choices_g2_q1_c2 <- switch(input$choose_g2_q1_b,
                                 "Fund A" = c("Fund B", "Fund C"),
                                 "Fund B" = c("Fund A", "Fund C"),
                                 "Fund C" = c("Fund A", "Fund B"))
      updateRadioButtons(session, "choose_g2_q1_c2", choices = choices_g2_q1_c2, selected = character(0))
    }
  })
  
  # Table: table_g2_q2_a ---------------------------------------
  
  
  
  df_g2_q2_a1 <- reactive(
    {
      
      specs <- get_first_2()
      data.frame(
        fund_no = c("Fund A"),
        type_fund = c("Target Date RILA"),
        target_date = as.integer(c(year_retire())),
        gross_expratio = percent(c(0.002), accuracy = 0.01),
        current_floor = percent(c(specs$f[2]), accuracy = 0.1),
        current_cap = percent(c(specs$fb[2]), accuracy = 0.1),
        middle_floor = percent(c(specs$m[2]), accuracy = 0.1),
        middle_cap = percent(c(specs$mb[2]), accuracy = 0.1),
        final_floor = percent(c(specs$l[2]), accuracy = 0.1),
        final_cap = percent(c(specs$lb[2]), accuracy = 0.1)
      )
    }
  )
  
  output$table_g2_q2_a1 <- renderTable({
    head(df_g2_q2_a1())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  df_g2_q2_a2 <- reactive(
    {
      
      specs <- get_first_2()
      data.frame(
        fund_no = c("Fund B", "Fund C"),
        type_fund = c("Target Date RILA", "Target Date RILA"),
        target_date = as.integer(c(year_retire(), year_retire())),
        gross_expratio = percent(c(0.002, 0.002), accuracy = 0.01),
        current_floor = percent(c(specs$f[1], specs$f[3]), accuracy = 0.1),
        current_cap = percent(c(specs$fb[1], specs$fb[3]), accuracy = 0.1),
        middle_floor = percent(c(specs$m[1], specs$m[3]), accuracy = 0.1),
        middle_cap = percent(c(specs$mb[1], specs$mb[3]), accuracy = 0.1),
        final_floor = percent(c(specs$l[1], specs$l[3]), accuracy = 0.1),
        final_cap = percent(c(specs$lb[1], specs$lb[3]), accuracy = 0.1)
      )})
  
  output$table_g2_q2_a2 <- renderTable({
    head(df_g2_q2_a2())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Table: table_g2_q2_b ---------------------------------------
  output$table_g2_q2_b1 <- renderTable({
    dat <- df_g2_q2_a1()
    if (input$choose_g2_q2_a == "Fund A") {
      dat$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01) # Gross Expense Ratio changes for fund selected in Part A.
      dat$current_cap = percent(update_current_cap_g1_q2_funda(), accuracy = 0.1)
      dat$middle_cap = percent(update_middle_cap_g1_q2_funda(), accuracy = 0.1)
      dat$final_cap = percent(update_final_cap_g1_q2_funda(), accuracy = 0.1) # Cap level updated based on fund selection in Part A and expense ratio.
    }
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  output$table_g2_q2_b2 <- renderTable({
    select_g2_q2_a <- switch(input$choose_g2_q2_a,
                             "Fund B" = 1,
                             "Fund C" = 2)
    dat <- df_g2_q2_a2()
    dat$gross_expratio[select_g2_q2_a] = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01) # Gross Expense Ratio changes for fund selected in Part A.
    
    if (isTruthy(select_g2_q2_a)) {
      
      dat$current_cap[select_g2_q2_a] = if (select_g2_q2_a == 1) {
        percent(update_current_cap_g1_q2_fundb(), accuracy = 0.1)
      } else {
        percent(update_current_cap_g1_q2_fundc(), accuracy = 0.1)
      }
      
      dat$middle_cap[select_g2_q2_a] = if (select_g2_q2_a == 1) {
        percent(update_middle_cap_g1_q2_fundb(), accuracy = 0.1)
      } else {
        percent(update_middle_cap_g1_q2_fundc(), accuracy = 0.1)
      }
      
      dat$final_cap[select_g2_q2_a] = if (select_g2_q2_a == 1) {
        percent(update_final_cap_g1_q2_fundb(), accuracy = 0.1)
      } else {
        percent(update_final_cap_g1_q2_fundc(), accuracy = 0.1)
      }# Cap level updated based on fund selection in Part A and expense ratio.
    }
    
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Choices: table_g2_q2_c2 ---------------------------------------
  observeEvent(input$button_to_g2_q2_c, {
    if (isTruthy(input$choose_g2_q2_b)) {
      choices_g2_q2_c2 <- switch(input$choose_g2_q2_b,
                                 "Fund A" = c("Fund B", "Fund C"),
                                 "Fund B" = c("Fund A", "Fund C"),
                                 "Fund C" = c("Fund A", "Fund B"))
      updateRadioButtons(session, "choose_g2_q2_c2", choices = choices_g2_q2_c2, selected = character(0))
    }
  })
  
  
  # Table: table_g2_q3_a ---------------------------------------
  
  
  df_g2_q3_a1 <- reactive(
    { 
      
      specs <- get_first_3()
      data.frame(
        fund_no = c("Fund A"),
        type_fund = c("Target Date RILA"),
        target_date = as.integer(c(year_retire())),
        gross_expratio = percent(c(0.002), accuracy = 0.01),
        current_buffer = percent(c(specs$f[2]), accuracy = 0.1),
        current_cap = percent(c(specs$fb[2]), accuracy = 0.1),
        middle_buffer = percent(c(specs$m[2]), accuracy = 0.1),
        middle_cap = percent(c(specs$mb[2]), accuracy = 0.1),
        final_buffer = percent(c(specs$l[2]), accuracy = 0.1),
        final_cap = percent(c(specs$lb[2]), accuracy = 0.1)
      )})
  output$table_g2_q3_a1 <- renderTable({
    head(df_g2_q3_a1())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  
  
  df_g2_q3_a2 <- reactive(
    {
      
      specs <- get_first_3()
      data.frame(
        fund_no = c("Fund B", "Fund C"),
        type_fund = c("Target Date RILA", "Target Date RILA"),
        target_date = as.integer(c(year_retire(), year_retire())),
        gross_expratio = percent(c(0.002, 0.002), accuracy = 0.01),
        current_buffer = percent(c(specs$f[1], specs$f[3]), accuracy = 0.1),
        current_cap = percent(c(specs$fb[1], specs$fb[3]), accuracy = 0.1),
        middle_buffer = percent(c(specs$m[1], specs$m[3]), accuracy = 0.1),
        middle_cap = percent(c(specs$mb[1], specs$mb[3]), accuracy = 0.1),
        final_buffer = percent(c(specs$l[1], specs$l[3]), accuracy = 0.1),
        final_cap = percent(c(specs$lb[1], specs$lb[3]), accuracy = 0.1)
      )
    }
  )
  output$table_g2_q3_a2 <- renderTable({
    head(df_g2_q3_a2())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Table: table_g2_q3_b ---------------------------------------
  output$table_g2_q3_b1 <- renderTable({
    dat <- df_g2_q3_a1()
    if (input$choose_g2_q3_a == "Fund A") {
      dat$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01) # Gross Expense Ratio changes for fund selected in Part A.
      dat$current_cap = percent(update_current_cap_g1_q3_funda(), accuracy = 0.1)
      dat$middle_cap = percent(update_middle_cap_g1_q3_funda(), accuracy = 0.1)
      dat$final_cap = percent(update_final_cap_g1_q3_funda(), accuracy = 0.1)# Cap level updated based on fund selection in Part A and expense ratio.
    }
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  output$table_g2_q3_b2 <- renderTable({
    select_g2_q3_a <- switch(input$choose_g2_q3_a,
                             "Fund B" = 1,
                             "Fund C" = 2)
    dat <- df_g2_q3_a2()
    
    dat$gross_expratio[select_g2_q3_a] = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01) # Gross Expense Ratio changes for fund selected in Part A.
    
    if (isTruthy(select_g2_q3_a)) {
      dat$current_cap[select_g2_q3_a] = if (select_g2_q3_a == 1) {
        percent(update_current_cap_g1_q3_fundb(), accuracy = 0.1)
      } else {
        percent(update_current_cap_g1_q3_fundc(), accuracy = 0.1)
      }
      
      dat$middle_cap[select_g2_q3_a] = if (select_g2_q3_a == 1) {
        percent(update_middle_cap_g1_q3_fundb(), accuracy = 0.1)
      } else {
        percent(update_middle_cap_g1_q3_fundc(), accuracy = 0.1)
      }
      
      dat$final_cap[select_g2_q3_a] = if (select_g2_q3_a == 1) {
        percent(update_final_cap_g1_q3_fundb(), accuracy = 0.1)
      } else {
        percent(update_final_cap_g1_q3_fundc(), accuracy = 0.1)
      }# Cap level updated based on fund selection in Part A and expense ratio.
    }
    
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Choices: table_g2_q3_c2 ---------------------------------------
  observeEvent(input$button_to_g2_q3_c, {
    if (isTruthy(input$choose_g2_q3_b)) {
      choices_g2_q3_c2 <- switch(input$choose_g2_q3_b,
                                 "Fund A" = c("Fund B", "Fund C"),
                                 "Fund B" = c("Fund A", "Fund C"),
                                 "Fund C" = c("Fund A", "Fund B"))
      updateRadioButtons(session, "choose_g2_q3_c2", choices = choices_g2_q3_c2, selected = character(0))
    }
  })
  
  # Table: table_g2_q4_a ---------------------------------------
  
  
  df_g2_q4_a1 <- reactive(
    {
      
      specs <- get_first_4a()
      data.frame(
        fund_no = c("Fund A"),
        type_fund = c("Target Date Fund"),
        target_date = as.integer(c(year_retire())),
        gross_expratio = percent(c(0.002), accuracy = 0.01),
        current_e = percent(c(specs$f[1]), accuracy = 1),
        current_fi = percent(c(specs$fb[1]), accuracy = 1),
        middle_e = percent(c(specs$m[1]), accuracy = 1),
        middle_fi = percent(c(specs$mb[1]), accuracy = 1),
        final_e = percent(c(specs$l[1]), accuracy = 1),
        final_fi = percent(c(specs$lb[1]), accuracy = 1)
      )
    }
  )
  
  output$table_g2_q4_a1 <- renderTable({
    head(df_g2_q4_a1())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation 
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income"
  ), 
  align = 'c')
  
  
  
  df_g2_q4_a2 <- reactive(
    {
      
      specs <- get_first_4b()
      
      data.frame(
        fund_no = c("Fund B"),
        type_fund = c("Target Date RILA"),
        target_date = as.integer(c(year_retire())),
        gross_expratio = percent(c(0.002), accuracy = 0.01),
        current_floor = percent(c(specs$f[1]), accuracy = 0.1),
        current_cap = percent(c(specs$fb[1]), accuracy = 0.1),
        middle_floor = percent(c(specs$m[1]), accuracy = 0.1),
        middle_cap = percent(c(specs$mb[1]), accuracy = 0.1),
        final_floor = percent(c(specs$l[1]), accuracy = 0.1),
        final_cap = percent(c(specs$lb[1]), accuracy = 0.1)
      )})
  
  output$table_g2_q4_a2 <- renderTable({
    
    head(df_g2_q4_a2())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  
  
  
  
  df_g2_q4_a3 <- reactive(
    {
      specs <- get_first_4c()
      data.frame(
        fund_no = c("Fund C"),
        type_fund = c("Target Date RILA"),
        target_date = as.integer(c(year_retire())),
        gross_expratio = percent(c(0.002), accuracy = 0.01),
        current_buffer = percent(c(specs$f[1]), accuracy = 0.1),
        current_cap = percent(c(specs$fb[1]), accuracy = 0.1),
        middle_buffer = percent(c(specs$m[1]), accuracy = 0.1),
        middle_cap = percent(c(specs$mb[1]), accuracy = 0.1),
        final_buffer = percent(c(specs$l[1]), accuracy = 0.1),
        final_cap = percent(c(specs$lb[1]), accuracy = 0.1)
      )})
  output$table_g2_q4_a3 <- renderTable({
    
    head(df_g2_q4_a3())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Table: table_g2_q4_b ---------------------------------------
  output$table_g2_q4_b1 <- renderTable({
    dat <- df_g2_q4_a1()
    if (input$choose_g2_q4_a == "Fund A") {
      dat$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01) # Gross Expense Ratio changes for fund selected in Part A.
    }
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation 
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income"
  ), 
  align = 'c')
  
  output$table_g2_q4_b2 <- renderTable({
    dat <- df_g2_q4_a2()
    if (input$choose_g2_q4_a == "Fund B") {
      dat$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01) # Gross Expense Ratio changes for fund selected in Part A.
      dat$current_cap = percent(update_current_cap_g1_q2_funda(), accuracy = 0.1)
      dat$middle_cap = percent(update_middle_cap_g1_q2_funda(), accuracy = 0.1)
      dat$final_cap = percent(update_final_cap_g1_q2_funda(), accuracy = 0.1)# Cap level updated based on fund selection in Part A and expense ratio.
    }
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  output$table_g2_q4_b3 <- renderTable({
    dat <- df_g2_q4_a3()
    if (input$choose_g2_q4_a == "Fund C") {
      dat$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01) # Gross Expense Ratio changes for fund selected in Part A.
      dat$current_cap = percent(update_current_cap_g1_q3_funda(), accuracy = 0.1)
      dat$middle_cap = percent(update_middle_cap_g1_q3_funda(), accuracy = 0.1)
      dat$final_cap = percent(update_final_cap_g1_q3_funda(), accuracy = 0.1) # Cap level updated based on fund selection in Part A and expense ratio.
    }
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  
  # Choices: table_g2_q4_c2 ---------------------------------------
  observeEvent(input$button_to_g2_q4_c, {
    if (isTruthy(input$choose_g2_q4_b)) {
      choices_g2_q4_c2 <- switch(input$choose_g2_q4_b,
                                 "Fund A" = c("Fund B", "Fund C"),
                                 "Fund B" = c("Fund A", "Fund C"),
                                 "Fund C" = c("Fund A", "Fund B"))
      updateRadioButtons(session, "choose_g2_q4_c2", choices = choices_g2_q4_c2, selected = character(0))
    }
  })
  
  
  # Table: table_g3_q1_a ---------------------------------------
  
  
  df_g3_q1_a <- reactive(
    {
      
      specs <- get_first_1()
      data.frame(
        fund_no = c("Fund A", "Fund B", "Fund C"),
        type_fund = c("Target Date Fund", "Target Date Fund", "Target Date Fund"),
        target_date = as.integer(c(year_retire(), year_retire(), year_retire())),
        gross_expratio = percent(c(0.002, 0.002, 0.002), accuracy = 0.01),
        current_e = percent(c(specs$f[1], specs$f[2], specs$f[3]), accuracy = 1),
        current_fi = percent(c(specs$fb[1], specs$fb[2], specs$fb[3]), accuracy = 1),
        middle_e = percent(c(specs$m[1], specs$m[2], specs$m[3]), accuracy = 1),
        middle_fi = percent(c(specs$mb[1], specs$mb[1], specs$mb[2]), accuracy = 1),
        final_e = percent(c(specs$l[1], specs$l[2], specs$l[3]), accuracy = 1),
        final_fi = percent(c(specs$lb[1], specs$lb[2], specs$lb[3]), accuracy = 1),
        retirebenefit_worst = dollar(c(specs$rbw[1], specs$rbw[2], specs$rbw[3]), accuracy = 1),
        retirebenefit_expected = dollar(c(specs$rbe[1], specs$rbe[2], specs$rbe[3]), accuracy = 1),
        retirebenefit_best = dollar(c(specs$rbb[1], specs$rbb[2], specs$rbb[3]), accuracy = 1),
        retirebenefit_sd = dollar(c(specs$rbs[1], specs$rbs[2], specs$rbs[3]), accuracy = 1)
      )
    }
  )
  
  output$table_g3_q1_a <- renderTable({
    head(df_g3_q1_a())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income "
  ), 
  align = 'c')
  
  # Table: table_g3_q1_b ---------------------------------------
  output$table_g3_q1_b <- renderTable({
    dat <- df_g3_q1_a()
    select_g3_q1_a <- switch(input$choose_g3_q1_a,
                             "Fund A" = 1,
                             "Fund B" = 2,
                             "Fund C" = 3)
    dat$gross_expratio[select_g3_q1_a] = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01)# Gross Expense Ratio changes for fund selected in Part A.
    
    dat$retirebenefit_worst[select_g3_q1_a] = if (select_g3_q1_a == 1) {
      dollar(update_retirebenefit_worst_g3_q1_funda(), accuracy = 1)
    } else if (select_g3_q1_a == 2) {
      dollar(update_retirebenefit_worst_g3_q1_fundb(), accuracy = 1)
    } else {
      dollar(update_retirebenefit_worst_g3_q1_fundc(), accuracy = 1)
    }
    
    dat$retirebenefit_expected[select_g3_q1_a] = if (select_g3_q1_a == 1) {
      dollar(update_retirebenefit_expected_g3_q1_funda(), accuracy = 1)
    } else if (select_g3_q1_a == 2) {
      dollar(update_retirebenefit_expected_g3_q1_fundb(), accuracy = 1)
    } else {
      dollar(update_retirebenefit_expected_g3_q1_fundc(), accuracy = 1)
    }
    
    dat$retirebenefit_best[select_g3_q1_a] = if (select_g3_q1_a == 1) {
      dollar(update_retirebenefit_best_g3_q1_funda(), accuracy = 1)
    } else if (select_g3_q1_a == 2) {
      dollar(update_retirebenefit_best_g3_q1_fundb(), accuracy = 1)
    } else {
      dollar(update_retirebenefit_best_g3_q1_fundc(), accuracy = 1)
    }
    
    dat$retirebenefit_sd[select_g3_q1_a] = if (select_g3_q1_a == 1) {
      dollar(update_retirebenefit_sd_g3_q1_funda(), accuracy = 1)
    } else if (select_g3_q1_a == 2) {
      dollar(update_retirebenefit_sd_g3_q1_fundb(), accuracy = 1)
    } else {
      dollar(update_retirebenefit_sd_g3_q1_fundc(), accuracy = 1)
    }# Retirement benefit based on fund selection in Part A and expense ratio.
    
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income "
  ), 
  align = 'c')
  
  # Choices: table_g3_q1_c2 ---------------------------------------
  observeEvent(input$button_to_g3_q1_c, {
    if (isTruthy(input$choose_g3_q1_b)) {
      choices_g3_q1_c2 <- switch(input$choose_g3_q1_b,
                                 "Fund A" = c("Fund B", "Fund C"),
                                 "Fund B" = c("Fund A", "Fund C"),
                                 "Fund C" = c("Fund A", "Fund B"))
      updateRadioButtons(session, "choose_g3_q1_c2", choices = choices_g3_q1_c2, selected = character(0))
    }
  })
  # Table: table_g3_q2_a ---------------------------------------
  
  df_g3_q2_a <- reactive({
    
    specs <- get_first_2()
    data.frame(
      fund_no = c("Fund A", "Fund B", "Fund C"),
      type_fund = c("Target Date RILA", "Target Date RILA", "Target Date RILA"),
      target_date = as.integer(c(year_retire(), year_retire(), year_retire())),
      gross_expratio = percent(c(0.002, 0.002, 0.002), accuracy = 0.01),
      current_floor = percent(c(specs$f[1], specs$f[2], specs$f[3]), accuracy = 0.1),
      current_cap = percent(c(specs$fb[1], specs$fb[2], specs$fb[3]), accuracy = 0.1),
      middle_floor = percent(c(specs$m[1], specs$m[2], specs$m[3]), accuracy = 0.1),
      middle_cap = percent(c(specs$mb[1], specs$mb[2], specs$mb[3]), accuracy = 0.1),
      final_floor = percent(c(specs$l[1], specs$l[2], specs$l[3]), accuracy = 0.1),
      final_cap = percent(c(specs$lb[1], specs$lb[2], specs$lb[3]), accuracy = 0.1),
      retirebenefit_worst = dollar(c(specs$rbw[1], specs$rbw[2], specs$rbw[3]), accuracy = 1),
      retirebenefit_expected = dollar(c(specs$rbe[1], specs$rbe[2], specs$rbe[3]), accuracy = 1),
      retirebenefit_best = dollar(c(specs$rbb[1], specs$rbb[2], specs$rbb[3]), accuracy = 1),
      retirebenefit_sd = dollar(c(specs$rbs[1], specs$rbs[2], specs$rbs[3]), accuracy = 1)
    )})
  
  output$table_g3_q2_a <- renderTable({
    head(df_g3_q2_a())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Table: table_g3_q2_b ---------------------------------------
  output$table_g3_q2_b <- renderTable({
    dat <- df_g3_q2_a()
    select_g3_q2_a <- switch(input$choose_g3_q2_a,
                             "Fund A" = 1,
                             "Fund B" = 2,
                             "Fund C" = 3)
    dat$gross_expratio[select_g3_q2_a] = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01) # Gross Expense Ratio changes for fund selected in Part A.
    
    dat$current_cap[select_g3_q2_a] = if (select_g3_q2_a == 1) {
      percent(update_current_cap_g1_q2_funda(), accuracy = 0.1)
    } else if (select_g3_q2_a == 2) {
      percent(update_current_cap_g1_q2_fundb(), accuracy = 0.1)
    } else {
      percent(update_current_cap_g1_q2_fundc(), accuracy = 0.1)
    }
    
    dat$middle_cap[select_g3_q2_a] = if (select_g3_q2_a == 1) {
      percent(update_middle_cap_g1_q2_funda(), accuracy = 0.1)
    } else if (select_g3_q2_a == 2) {
      percent(update_middle_cap_g1_q2_fundb(), accuracy = 0.1)
    } else {
      percent(update_middle_cap_g1_q2_fundc(), accuracy = 0.1)
    }
    
    dat$final_cap[select_g3_q2_a] = if (select_g3_q2_a == 1) {
      percent(update_final_cap_g1_q2_funda(), accuracy = 0.1)
    } else if (select_g3_q2_a == 2) {
      percent(update_final_cap_g1_q2_fundb(), accuracy = 0.1)
    } else {
      percent(update_final_cap_g1_q2_fundc(), accuracy = 0.1)
    }# Cap level updated based on fund selection in Part A and expense ratio.
    
    dat$retirebenefit_worst[select_g3_q2_a] = if (select_g3_q2_a == 1) {
      dollar(update_retirebenefit_worst_g3_q2_funda(), accuracy = 1)
    } else if (select_g3_q2_a == 2) {
      dollar(update_retirebenefit_worst_g3_q2_fundb(), accuracy = 1)
    } else {
      dollar(update_retirebenefit_worst_g3_q2_fundc(), accuracy = 1)
    }
    
    dat$retirebenefit_expected[select_g3_q2_a] = if (select_g3_q2_a == 1) {
      dollar(update_retirebenefit_expected_g3_q2_funda(), accuracy = 1)
    } else if (select_g3_q2_a == 2) {
      dollar(update_retirebenefit_expected_g3_q2_fundb(), accuracy = 1)
    } else {
      dollar(update_retirebenefit_expected_g3_q2_fundc(), accuracy = 1)
    }
    
    dat$retirebenefit_best[select_g3_q2_a] = if (select_g3_q2_a == 1) {
      dollar(update_retirebenefit_best_g3_q2_funda(), accuracy = 1)
    } else if (select_g3_q2_a == 2) {
      dollar(update_retirebenefit_best_g3_q2_fundb(), accuracy = 1)
    } else {
      dollar(update_retirebenefit_best_g3_q2_fundc(), accuracy = 1)
    }
    
    dat$retirebenefit_sd[select_g3_q2_a] = if (select_g3_q2_a == 1) {
      dollar(update_retirebenefit_sd_g3_q2_funda(), accuracy = 1)
    } else if (select_g3_q2_a == 2) {
      dollar(update_retirebenefit_sd_g3_q2_fundb(), accuracy = 1)
    } else {
      dollar(update_retirebenefit_sd_g3_q2_fundc(), accuracy = 1)
    }# Retirement benefit based on fund selection in Part A and expense ratio.
    
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  # Choices: table_g3_q2_c2 ---------------------------------------
  observeEvent(input$button_to_g3_q2_c, {
    if (isTruthy(input$choose_g3_q2_b)) {
      choices_g3_q2_c2 <- switch(input$choose_g3_q2_b,
                                 "Fund A" = c("Fund B", "Fund C"),
                                 "Fund B" = c("Fund A", "Fund C"),
                                 "Fund C" = c("Fund A", "Fund B"))
      updateRadioButtons(session, "choose_g3_q2_c2", choices = choices_g3_q2_c2, selected = character(0))
    }
  })
  # Table: table_g3_q3_a ---------------------------------------
  
  df_g3_q3_a <- reactive({
    specs <- get_first_3()
    data.frame(
      fund_no = c("Fund A", "Fund B", "Fund C"),
      type_fund = c("Target Date RILA", "Target Date RILA", "Target Date RILA"),
      target_date = as.integer(c(year_retire(), year_retire(), year_retire())),
      gross_expratio = percent(c(0.002, 0.002, 0.002), accuracy = 0.01),
      current_buffer = percent(c(specs$f[1], specs$f[2], specs$f[3]), accuracy = 0.1),
      current_cap = percent(c(specs$fb[1], specs$fb[2], specs$fb[3]), accuracy = 0.1),
      middle_buffer = percent(c(specs$m[1], specs$m[2], specs$m[3]), accuracy = 0.1),
      middle_cap = percent(c(specs$mb[1], specs$mb[2], specs$mb[3]), accuracy = 0.1),
      final_buffer = percent(c(specs$l[1], specs$l[2], specs$l[3]), accuracy = 0.1),
      final_cap = percent(c(specs$lb[1], specs$lb[2], specs$lb[3]), accuracy = 0.1),
      retirebenefit_worst = dollar(c(specs$rbw[1], specs$rbw[2], specs$rbw[3]), accuracy = 1),
      retirebenefit_expected = dollar(c(specs$rbe[1], specs$rbe[2], specs$rbe[3]), accuracy = 1),
      retirebenefit_best = dollar(c(specs$rbb[1], specs$rbb[2], specs$rbb[3]), accuracy = 1),
      retirebenefit_sd = dollar(c(specs$rbs[1], specs$rbs[2], specs$rbs[3]), accuracy = 1)
    )})
  
  
  output$table_g3_q3_a <- renderTable({
    
    head(df_g3_q3_a())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)  </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Table: table_g3_q3_b ---------------------------------------
  output$table_g3_q3_b <- renderTable({
    dat <- df_g3_q3_a()
    select_g3_q3_a <- switch(input$choose_g3_q3_a,
                             "Fund A" = 1,
                             "Fund B" = 2,
                             "Fund C" = 3)
    dat$gross_expratio[select_g3_q3_a] = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01)# Gross Expense Ratio changes for fund selected in Part A.
    
    dat$current_cap[select_g3_q3_a] = if (select_g3_q3_a == 1) {
      percent(update_current_cap_g1_q3_funda(), accuracy = 0.1) 
    } else if (select_g3_q3_a == 2) {
      percent(update_current_cap_g1_q3_fundb(), accuracy = 0.1) 
    } else {
      percent(update_current_cap_g1_q3_fundc(), accuracy = 0.1) 
    }
    
    dat$middle_cap[select_g3_q3_a] = if (select_g3_q3_a == 1) {
      percent(update_middle_cap_g1_q3_funda(), accuracy = 0.1) 
    } else if (select_g3_q3_a == 2) {
      percent(update_middle_cap_g1_q3_fundb(), accuracy = 0.1) 
    } else {
      percent(update_middle_cap_g1_q3_fundc(), accuracy = 0.1) 
    }
    
    dat$final_cap[select_g3_q3_a] = if (select_g3_q3_a == 1) {
      percent(update_final_cap_g1_q3_funda(), accuracy = 0.1) 
    } else if (select_g3_q3_a == 2) {
      percent(update_final_cap_g1_q3_fundb(), accuracy = 0.1) 
    } else {
      percent(update_final_cap_g1_q3_fundc(), accuracy = 0.1) 
    }# Cap level updated based on fund selection in Part A and expense ratio.
    
    dat$retirebenefit_worst[select_g3_q3_a] = if (select_g3_q3_a == 1) {
      dollar(update_retirebenefit_worst_g3_q3_funda(), accuracy = 1)
    } else if (select_g3_q3_a == 2) {
      dollar(update_retirebenefit_worst_g3_q3_fundb(), accuracy = 1)
    } else {
      dollar(update_retirebenefit_worst_g3_q3_fundc(), accuracy = 1)
    }
    
    dat$retirebenefit_expected[select_g3_q3_a] = if (select_g3_q3_a == 1) {
      dollar(update_retirebenefit_expected_g3_q3_funda(), accuracy = 1)
    } else if (select_g3_q3_a == 2) {
      dollar(update_retirebenefit_expected_g3_q3_fundb(), accuracy = 1)
    } else {
      dollar(update_retirebenefit_expected_g3_q3_fundc(), accuracy = 1)
    }
    
    dat$retirebenefit_best[select_g3_q3_a] = if (select_g3_q3_a == 1) {
      dollar(update_retirebenefit_best_g3_q3_funda(), accuracy = 1)
    } else if (select_g3_q3_a == 2) {
      dollar(update_retirebenefit_best_g3_q3_fundb(), accuracy = 1)
    } else {
      dollar(update_retirebenefit_best_g3_q3_fundc(), accuracy = 1)
    }
    
    dat$retirebenefit_sd[select_g3_q3_a] = if (select_g3_q3_a == 1) {
      dollar(update_retirebenefit_sd_g3_q3_funda(), accuracy = 1)
    } else if (select_g3_q3_a == 2) {
      dollar(update_retirebenefit_sd_g3_q3_fundb(), accuracy = 1)
    } else {
      dollar(update_retirebenefit_sd_g3_q3_fundc(), accuracy = 1)
    }# Retirement benefit based on fund selection in Part A and expense ratio.
    
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)  </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Choices: table_g3_q3_c2 ---------------------------------------
  observeEvent(input$button_to_g3_q3_c, {
    if (isTruthy(input$choose_g3_q3_b)) {
      choices_g3_q3_c2 <- switch(input$choose_g3_q3_b,
                                 "Fund A" = c("Fund B", "Fund C"),
                                 "Fund B" = c("Fund A", "Fund C"),
                                 "Fund C" = c("Fund A", "Fund B"))
      updateRadioButtons(session, "choose_g3_q3_c2", choices = choices_g3_q3_c2, selected = character(0))
    }
  })
  
  
  # Table: table_g3_q4_a ---------------------------------------
  
  df_g3_q4_a1 <- reactive({
    specs <- get_first_4a()
    data.frame(
      fund_no = c("Fund A"),
      type_fund = c("Target Date Fund"),
      target_date = as.integer(c(year_retire())),
      gross_expratio = percent(c(0.002), accuracy = 0.01),
      current_e = percent(c(specs$f[1]), accuracy = 1),
      current_fi = percent(c(specs$fb[1]), accuracy = 1),
      middle_e = percent(c(specs$m[1]), accuracy = 1),
      middle_fi = percent(c(specs$mb[1]), accuracy = 1),
      final_e = percent(c(specs$l[1]), accuracy = 1),
      final_fi = percent(c(specs$lb[1]), accuracy = 1),
      retirebenefit_worst = dollar(specs$rbw[1], accuracy = 1),
      retirebenefit_expected = dollar(specs$rbe[1], accuracy = 1),
      retirebenefit_best = dollar(specs$rbb[1], accuracy = 1),
      retirebenefit_sd = dollar(specs$rbs[1], accuracy = 1)
    )})
  
  output$table_g3_q4_a1 <- renderTable({
    
    head(df_g3_q4_a1())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation  </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income"
  ), 
  align = 'c')
  
  df_g3_q4_a2 <- reactive({
    specs <- get_first_4b()
    data.frame(
      fund_no = c("Fund B"),
      type_fund = c("Target Date RILA"),
      target_date = as.integer(c(year_retire())),
      gross_expratio = percent(c(0.002), accuracy = 0.01),
      current_floor = percent(c(specs$f[1]), accuracy = 0.1),
      current_cap = percent(c(specs$fb[1]), accuracy = 0.1),
      middle_floor = percent(c(specs$m[1]), accuracy = 0.1),
      middle_cap = percent(c(specs$mb[1]), accuracy = 0.1),
      final_floor = percent(c(specs$l[1]), accuracy = 0.1),
      final_cap = percent(c(specs$lb[1]), accuracy = 0.1),
      retirebenefit_worst = dollar(specs$rbw[1], accuracy = 1),
      retirebenefit_expected = dollar(specs$rbe[1], accuracy = 1),
      retirebenefit_best = dollar(specs$rbb[1], accuracy = 1),
      retirebenefit_sd = dollar(specs$rbs[1], accuracy = 1)
    )})
  
  output$table_g3_q4_a2 <- renderTable({
    head(df_g3_q4_a2())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  
  
  
  df_g3_q4_a3 <- reactive({
    specs <- get_first_4c()
    data.frame(
      fund_no = c("Fund C"),
      type_fund = c("Target Date RILA"),
      target_date = as.integer(c(year_retire())),
      gross_expratio = percent(c(0.002), accuracy = 0.01),
      current_buffer = percent(c(specs$f[1]), accuracy = 0.1),
      current_cap = percent(c(specs$fb[1]), accuracy = 0.1),
      middle_buffer = percent(c(specs$m[1]), accuracy = 0.1),
      middle_cap = percent(c(specs$mb[1]), accuracy = 0.1),
      final_buffer = percent(c(specs$l[1]), accuracy = 0.1),
      final_cap = percent(c(specs$lb[1]), accuracy = 0.1),
      retirebenefit_worst = dollar(specs$rbw[1], accuracy = 1),
      retirebenefit_expected = dollar(specs$rbe[1], accuracy = 1),
      retirebenefit_best = dollar(specs$rbe[1], accuracy = 1),
      retirebenefit_sd = dollar(specs$rbs[1], accuracy = 1)
    )})
  
  output$table_g3_q4_a3 <- renderTable({
    
    head(df_g3_q4_a3())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Table: table_g3_q4_b ---------------------------------------
  output$table_g3_q4_b1 <- renderTable({
    dat <- df_g3_q4_a1()
    if (input$choose_g3_q4_a == "Fund A") {
      dat$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01)# Gross Expense Ratio changes for fund selected in Part A.
      dat$retirebenefit_worst = dollar(update_retirebenefit_worst_g3_q1_funda(), accuracy = 1)
      dat$retirebenefit_expected = dollar(update_retirebenefit_expected_g3_q1_funda(), accuracy = 1)
      dat$retirebenefit_best = dollar(update_retirebenefit_best_g3_q1_funda(), accuracy = 1)
      dat$retirebenefit_sd = dollar(update_retirebenefit_sd_g3_q1_funda(), accuracy = 1)# Retirement benefit based on fund selection in Part A and expense ratio.
    }
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income"
  ), 
  align = 'c')
  
  output$table_g3_q4_b2 <- renderTable({
    dat <- df_g3_q4_a2()
    if (input$choose_g3_q4_a == "Fund B") {
      dat$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01)# Gross Expense Ratio changes for fund selected in Part A.
      dat$current_cap = percent(update_current_cap_g1_q2_funda(), accuracy = 0.1)
      dat$middle_cap = percent(update_middle_cap_g1_q2_funda(), accuracy = 0.1)
      dat$final_cap = percent(update_final_cap_g1_q2_funda(), accuracy = 0.1)# Cap level updated based on fund selection in Part A and expense ratio.
      dat$retirebenefit_worst = dollar(update_retirebenefit_worst_g3_q2_funda(), accuracy = 1)
      dat$retirebenefit_expected = dollar(update_retirebenefit_expected_g3_q2_funda(), accuracy = 1)
      dat$retirebenefit_best = dollar(update_retirebenefit_best_g3_q2_funda(), accuracy = 1)
      dat$retirebenefit_sd = dollar(update_retirebenefit_sd_g3_q2_funda(), accuracy = 1)# Retirement benefit based on fund selection in Part A and expense ratio.
    }
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  output$table_g3_q4_b3 <- renderTable({
    dat <- df_g3_q4_a3()
    if (input$choose_g3_q4_a == "Fund C") {
      dat$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01)# Gross Expense Ratio changes for fund selected in Part A.
      dat$current_cap = percent(update_current_cap_g1_q3_funda(), accuracy = 0.1)
      dat$middle_cap = percent(update_middle_cap_g1_q3_funda(), accuracy = 0.1)
      dat$final_cap = percent(update_final_cap_g1_q3_funda(), accuracy = 0.1)# Cap level updated based on fund selection in Part A and expense ratio.
      dat$retirebenefit_worst = dollar(update_retirebenefit_worst_g3_q3_funda(), accuracy = 1)
      dat$retirebenefit_expected = dollar(update_retirebenefit_expected_g3_q3_funda(), accuracy = 1)
      dat$retirebenefit_best = dollar(update_retirebenefit_best_g3_q3_funda(), accuracy = 1)
      dat$retirebenefit_sd = dollar(update_retirebenefit_sd_g3_q3_funda(), accuracy = 1)# Retirement benefit based on fund selection in Part A and expense ratio. 
    }
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Choices: table_g3_q4_c2 ---------------------------------------
  observeEvent(input$button_to_g3_q4_c, {
    if (isTruthy(input$choose_g3_q4_b)) {
      choices_g3_q4_c2 <- switch(input$choose_g3_q4_b,
                                 "Fund A" = c("Fund B", "Fund C"),
                                 "Fund B" = c("Fund A", "Fund C"),
                                 "Fund C" = c("Fund A", "Fund B"))
      updateRadioButtons(session, "choose_g3_q4_c2", choices = choices_g3_q4_c2, selected = character(0))
    }
  })
  
  
  # Table: table_g4_q1_a ---------------------------------------
  
  df_g4_q1_a1 <- reactive({
    specs <- get_first_1()
    data.frame(
      fund_no = c("Fund A"),
      type_fund = c("Target Date Fund"),
      target_date = as.integer(c(year_retire())),
      gross_expratio = percent(c(0.002), accuracy = 0.01),
      current_e = percent(c(specs$f[2]), accuracy = 1),
      current_fi = percent(c(specs$fb[2]), accuracy = 1),
      middle_e = percent(c(specs$m[2]), accuracy = 1),
      middle_fi = percent(c(specs$mb[2]), accuracy = 1),
      final_e = percent(c(specs$l[2]), accuracy = 1),
      final_fi = percent(c(specs$lb[2]), accuracy = 1),
      retirebenefit_worst = dollar(c(specs$rbw[2]), accuracy = 1),
      retirebenefit_expected = dollar(c(specs$rbe[2]), accuracy = 1),
      retirebenefit_best = dollar(c(specs$rbb[2]), accuracy = 1),
      retirebenefit_sd = dollar(c(specs$rbs[2]), accuracy = 1)
    )})
  
  output$table_g4_q1_a1 <- renderTable({
    
    head(df_g4_q1_a1())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income "
  ), 
  align = 'c')
  
  
  df_g4_q1_a2 <- reactive({
    specs <- get_first_1()
    data.frame(
      fund_no = c("Fund B", "Fund C"),
      type_fund = c("Target Date Fund", "Target Date Fund"),
      target_date = as.integer(c(year_retire(), year_retire())),
      gross_expratio = percent(c(0.002, 0.002), accuracy = 0.01),
      current_e = percent(c(specs$f[1], specs$f[3]), accuracy = 1),
      current_fi = percent(c(specs$fb[1], specs$fb[3]), accuracy = 1),
      middle_e = percent(c(specs$m[1], specs$m[3]), accuracy = 1),
      middle_fi = percent(c(specs$mb[1], specs$mb[3]), accuracy = 1),
      final_e = percent(c(specs$l[1], specs$l[3]), accuracy = 1),
      final_fi = percent(c(specs$lb[1], specs$lb[3]), accuracy = 1),
      retirebenefit_worst = dollar(c(specs$rbw[1], specs$rbw[3]), accuracy = 1),
      retirebenefit_expected = dollar(c(specs$rbe[1], specs$rbe[3]), accuracy = 1),
      retirebenefit_best = dollar(c(specs$rbb[1], specs$rbb[3]), accuracy = 1),
      retirebenefit_sd = dollar(c(specs$rbs[1], specs$rbs[3]), accuracy = 1)
    )})
  
  output$table_g4_q1_a2 <- renderTable({
    
    head(df_g4_q1_a2())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income "
  ), 
  align = 'c')
  
  # Table: table_g4_q1_b ---------------------------------------
  output$table_g4_q1_b1 <- renderTable({
    dat <- df_g4_q1_a1()
    if (input$choose_g4_q1_a == "Fund A") {
      dat$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01)# Gross Expense Ratio changes for fund selected in Part A.
      dat$retirebenefit_worst = dollar(update_retirebenefit_worst_g3_q1_funda(), accuracy = 1)
      dat$retirebenefit_expected = dollar(update_retirebenefit_expected_g3_q1_funda(), accuracy = 1)
      dat$retirebenefit_best = dollar(update_retirebenefit_best_g3_q1_funda(), accuracy = 1)
      dat$retirebenefit_sd = dollar(update_retirebenefit_sd_g3_q1_funda(), accuracy = 1) # Retirement benefit based on fund selection in Part A and expense ratio.
    }
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income "
  ), 
  align = 'c')
  
  output$table_g4_q1_b2 <- renderTable({
    dat <- df_g4_q1_a2()
    select_g4_q1_a <- switch(input$choose_g4_q1_a,
                             "Fund B" = 1,
                             "Fund C" = 2)
    dat$gross_expratio[select_g4_q1_a] = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01)# Gross Expense Ratio changes for fund selected in Part A.
    
    if (isTruthy(select_g4_q1_a)) {
      
      dat$retirebenefit_worst[select_g4_q1_a] = if (select_g4_q1_a == 1) {
        dollar(update_retirebenefit_worst_g3_q1_fundb(), accuracy = 1)
      } else {
        dollar(update_retirebenefit_worst_g3_q1_fundc(), accuracy = 1)
      }
      
      dat$retirebenefit_expected[select_g4_q1_a] = if (select_g4_q1_a == 1) {
        dollar(update_retirebenefit_expected_g3_q1_fundb(), accuracy = 1)
      } else {
        dollar(update_retirebenefit_expected_g3_q1_fundc(), accuracy = 1)
      }
      
      dat$retirebenefit_best[select_g4_q1_a] = if (select_g4_q1_a == 1) {
        dollar(update_retirebenefit_best_g3_q1_fundb(), accuracy = 1)
      } else {
        dollar(update_retirebenefit_best_g3_q1_fundc(), accuracy = 1)
      }
      
      dat$retirebenefit_sd[select_g4_q1_a] = if (select_g4_q1_a == 1) {
        dollar(update_retirebenefit_sd_g3_q1_fundb(), accuracy = 1)
      } else {
        dollar(update_retirebenefit_sd_g3_q1_fundc(), accuracy = 1)
      }# Retirement benefit based on fund selection in Part A and expense ratio.
    }
    
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income "
  ), 
  align = 'c')
  # Choices: table_g4_q1_c2 ---------------------------------------
  observeEvent(input$button_to_g4_q1_c, {
    if (isTruthy(input$choose_g4_q1_b)) {
      choices_g4_q1_c2 <- switch(input$choose_g4_q1_b,
                                 "Fund A" = c("Fund B", "Fund C"),
                                 "Fund B" = c("Fund A", "Fund C"),
                                 "Fund C" = c("Fund A", "Fund B"))
      updateRadioButtons(session, "choose_g4_q1_c2", choices = choices_g4_q1_c2, selected = character(0))
    }
  })
  # Table: table_g4_q2_a ---------------------------------------
  
  
  df_g4_q2_a1 <- reactive({
    specs <- get_first_2()
    data.frame(
      fund_no = c("Fund A"),
      type_fund = c("Target Date RILA"),
      target_date = as.integer(c(year_retire())),
      gross_expratio = percent(c(0.002), accuracy = 0.01),
      current_floor = percent(c(specs$f[2]), accuracy = 0.1),
      current_cap = percent(c(specs$fb[2]), accuracy = 0.1),
      middle_floor = percent(c(specs$m[2]), accuracy = 0.1),
      middle_cap = percent(c(specs$mb[2]), accuracy = 0.1),
      final_floor = percent(c(specs$l[2]), accuracy = 0.1),
      final_cap = percent(c(specs$lb[2]), accuracy = 0.1),
      retirebenefit_worst = dollar(c(specs$rbw[2]), accuracy = 1),
      retirebenefit_expected = dollar(c(specs$rbe[2]), accuracy = 1),
      retirebenefit_best = dollar(c(specs$rbb[2]), accuracy = 1),
      retirebenefit_sd = dollar(c(specs$rbs[2]), accuracy = 1)
    )})
  output$table_g4_q2_a1 <- renderTable({
    
    head(df_g4_q2_a1())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  
  df_g4_q2_a2 <- reactive({
    specs <- get_first_2()
    data.frame(
      fund_no = c("Fund B", "Fund C"),
      type_fund = c("Target Date RILA", "Target Date RILA"),
      target_date = as.integer(c(year_retire(), year_retire())),
      gross_expratio = percent(c(0.002, 0.002), accuracy = 0.01),
      current_floor = percent(c(specs$f[1], specs$f[3]), accuracy = 0.1),
      current_cap = percent(c(specs$fb[1], specs$fb[3]), accuracy = 0.1),
      middle_floor = percent(c(specs$m[1], specs$m[3]), accuracy = 0.1),
      middle_cap = percent(c(specs$mb[1], specs$mb[3]), accuracy = 0.1),
      final_floor = percent(c(specs$l[1], specs$l[3]), accuracy = 0.1),
      final_cap = percent(c(specs$lb[1], specs$lb[3]), accuracy = 0.1),
      retirebenefit_worst = dollar(c(specs$rbw[1], specs$rbw[3]), accuracy = 1),
      retirebenefit_expected = dollar(c(specs$rbe[1], specs$rbe[3]), accuracy = 1),
      retirebenefit_best = dollar(c(specs$rbb[1], specs$rbb[3]), accuracy = 1),
      retirebenefit_sd = dollar(c(specs$rbs[1], specs$rbs[3]), accuracy = 1)
    )})
  
  output$table_g4_q2_a2 <- renderTable({
    
    head(df_g4_q2_a2())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Table: table_g4_q2_b ---------------------------------------
  output$table_g4_q2_b1 <- renderTable({
    dat <- df_g4_q2_a1()
    if (input$choose_g4_q2_a == "Fund A") {
      dat$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01)# Gross Expense Ratio changes for fund selected in Part A.
      dat$current_cap = percent(update_current_cap_g1_q2_funda(), accuracy = 0.1)
      dat$middle_cap = percent(update_middle_cap_g1_q2_funda(), accuracy = 0.1)
      dat$final_cap = percent(update_final_cap_g1_q2_funda(), accuracy = 0.1)# Cap level updated based on fund selection in Part A and expense ratio.
      dat$retirebenefit_worst = dollar(update_retirebenefit_worst_g3_q2_funda(), accuracy = 1)
      dat$retirebenefit_expected = dollar(update_retirebenefit_expected_g3_q2_funda(), accuracy = 1)
      dat$retirebenefit_best = dollar(update_retirebenefit_best_g3_q2_funda(), accuracy = 1)
      dat$retirebenefit_sd = dollar(update_retirebenefit_sd_g3_q2_funda(), accuracy = 1)# Retirement benefit based on fund selection in Part A and expense ratio.
    }
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  output$table_g4_q2_b2 <- renderTable({
    dat <- df_g4_q2_a2()
    select_g4_q2_a <- switch(input$choose_g4_q2_a,
                             "Fund B" = 1,
                             "Fund C" = 2)
    dat$gross_expratio[select_g4_q2_a] = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01)# Cap level updated based on fund selection in Part A and expense ratio.
    
    if (isTruthy(select_g4_q2_a)) {
      
      dat$current_cap[select_g4_q2_a] = if (select_g4_q2_a == 1) {
        percent(update_current_cap_g1_q2_fundb(), accuracy = 0.1)
      } else {
        percent(update_current_cap_g1_q2_fundc(), accuracy = 0.1)
      }
      
      dat$middle_cap[select_g4_q2_a] = if (select_g4_q2_a == 1) {
        percent(update_middle_cap_g1_q2_fundb(), accuracy = 0.1)
      } else {
        percent(update_middle_cap_g1_q2_fundc(), accuracy = 0.1)
      }
      dat$final_cap[select_g4_q2_a] = if (select_g4_q2_a == 1) {
        percent(update_final_cap_g1_q2_fundb(), accuracy = 0.1)
      } else {
        percent(update_final_cap_g1_q2_fundc(), accuracy = 0.1)
      }# Cap level updated based on fund selection in Part A and expense ratio.
      
      dat$retirebenefit_worst[select_g4_q2_a] = if (select_g4_q2_a == 1) {
        dollar(update_retirebenefit_worst_g3_q2_fundb(), accuracy = 1)
      } else {
        dollar(update_retirebenefit_worst_g3_q2_fundc(), accuracy = 1)
      }
      
      dat$retirebenefit_expected[select_g4_q2_a] = if (select_g4_q2_a == 1) {
        dollar(update_retirebenefit_expected_g3_q2_fundb(), accuracy = 1)
      } else {
        dollar(update_retirebenefit_expected_g3_q2_fundc(), accuracy = 1)
      }
      
      dat$retirebenefit_best[select_g4_q2_a] = if (select_g4_q2_a == 1) {
        dollar(update_retirebenefit_best_g3_q2_fundb(), accuracy = 1)
      } else {
        dollar(update_retirebenefit_best_g3_q2_fundc(), accuracy = 1)
      }
      
      dat$retirebenefit_sd[select_g4_q2_a] = if (select_g4_q2_a == 1) {
        dollar(update_retirebenefit_sd_g3_q2_fundb(), accuracy = 1)
      } else {
        dollar(update_retirebenefit_sd_g3_q2_fundc(), accuracy = 1)
      }# Retirement benefit based on fund selection in Part A and expense ratio.
    }
    
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  # Choices: table_g4_q2_c2 ---------------------------------------
  observeEvent(input$button_to_g4_q2_c, {
    if (isTruthy(input$choose_g4_q2_b)) {
      choices_g4_q2_c2 <- switch(input$choose_g4_q2_b,
                                 "Fund A" = c("Fund B", "Fund C"),
                                 "Fund B" = c("Fund A", "Fund C"),
                                 "Fund C" = c("Fund A", "Fund B"))
      updateRadioButtons(session, "choose_g4_q2_c2", choices = choices_g4_q2_c2, selected = character(0))
    }
  })
  
  # Table: table_g4_q3_a ---------------------------------------
  
  df_g4_q3_a1 <- reactive({
    specs <- get_first_3()
    data.frame(
      fund_no = c("Fund A"),
      type_fund = c("Target Date RILA"),
      target_date = as.integer(c(year_retire())),
      gross_expratio = percent(c(0.002), accuracy = 0.01),
      current_buffer = percent(c(specs$f[2]), accuracy = 0.1),
      current_cap = percent(c(specs$fb[2]), accuracy = 0.1),
      middle_buffer = percent(c(specs$m[2]), accuracy = 0.1),
      middle_cap = percent(c(specs$mb[2]), accuracy = 0.1),
      final_buffer = percent(c(specs$l[2]), accuracy = 0.1),
      final_cap = percent(c(specs$lb[2]), accuracy = 0.1),
      retirebenefit_worst = dollar(c(specs$rbw[2]), accuracy = 1),
      retirebenefit_expected = dollar(c(specs$rbe[2]), accuracy = 1),
      retirebenefit_best = dollar(c(specs$rbb[2]), accuracy = 1),
      retirebenefit_sd = dollar(c(specs$rbs[2]), accuracy = 1)
    )})
  
  output$table_g4_q3_a1 <- renderTable({
    
    head(df_g4_q3_a1())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)  </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  
  
  df_g4_q3_a2 <- reactive({
    
    specs <- get_first_3()
    data.frame(
      fund_no = c("Fund B", "Fund C"),
      type_fund = c("Target Date RILA", "Target Date RILA"),
      target_date = as.integer(c(year_retire(), year_retire())),
      gross_expratio = percent(c(0.002, 0.002), accuracy = 0.01),
      current_buffer = percent(c(specs$f[1], specs$f[1]), accuracy = 0.1),
      current_cap = percent(c(specs$fb[1], specs$fb[1]), accuracy = 0.1),
      middle_buffer = percent(c(specs$m[1], specs$m[3]), accuracy = 0.1),
      middle_cap = percent(c(specs$mb[1], specs$mb[3]), accuracy = 0.1),
      final_buffer = percent(c(specs$l[1], specs$l[3]), accuracy = 0.1),
      final_cap = percent(c(specs$lb[1], specs$lb[3]), accuracy = 0.1),
      retirebenefit_worst = dollar(c(specs$rbw[1], specs$rbw[3]), accuracy = 1),
      retirebenefit_expected = dollar(c(specs$rbe[1], specs$rbe[3]), accuracy = 1),
      retirebenefit_best = dollar(c(specs$rbb[1], specs$rbb[3]), accuracy = 1),
      retirebenefit_sd = dollar(c(specs$rbs[1], specs$rbs[3]), accuracy = 1)
    )})
  
  
  output$table_g4_q3_a2 <- renderTable({
    
    head(df_g4_q3_a2())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)  </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  # Table: table_g4_q3_b ---------------------------------------
  output$table_g4_q3_b1 <- renderTable({
    dat <- df_g4_q3_a1()
    if (input$choose_g4_q3_a == "Fund A") {
      dat$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01)# Gross Expense Ratio changes for fund selected in Part A.
      dat$current_cap = percent(update_current_cap_g1_q3_funda(), accuracy = 0.1)
      dat$middle_cap = percent(update_middle_cap_g1_q3_funda() , accuracy = 0.1)
      dat$final_cap = percent(update_final_cap_g1_q3_funda(), accuracy = 0.1)# Cap level updated based on fund selection in Part A and expense ratio.
      dat$retirebenefit_worst = dollar(update_retirebenefit_worst_g3_q3_funda(), accuracy = 1)
      dat$retirebenefit_expected = dollar(update_retirebenefit_expected_g3_q3_funda(), accuracy = 1)
      dat$retirebenefit_best = dollar(update_retirebenefit_best_g3_q3_funda(), accuracy = 1)
      dat$retirebenefit_sd = dollar(update_retirebenefit_sd_g3_q3_funda(), accuracy = 1)# Retirement benefit based on fund selection in Part A and expense ratio.
    }
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)  </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  output$table_g4_q3_b2 <- renderTable({
    dat <- df_g4_q3_a2()
    select_g4_q3_a <- switch(input$choose_g4_q3_a,
                             "Fund B" = 1,
                             "Fund C" = 2)
    dat$gross_expratio[select_g4_q3_a] = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01)# Gross Expense Ratio changes for fund selected in Part A.
    
    if (isTruthy(select_g4_q3_a)) {
      
      dat$current_cap[select_g4_q3_a] = if (select_g4_q3_a == 1) {
        percent(update_current_cap_g1_q3_fundb(), accuracy = 0.1)
      } else {
        percent(update_current_cap_g1_q3_fundc(), accuracy = 0.1)
      }
      
      dat$middle_cap[select_g4_q3_a] = if (select_g4_q3_a == 1) {
        percent(update_middle_cap_g1_q3_fundb(), accuracy = 0.1)
      } else {
        percent(update_middle_cap_g1_q3_fundc(), accuracy = 0.1)
      }
      
      dat$final_cap[select_g4_q3_a] = if (select_g4_q3_a == 1) {
        percent(update_final_cap_g1_q3_fundb(), accuracy = 0.1)
      } else {
        percent(update_final_cap_g1_q3_fundc(), accuracy = 0.1)
      }# Cap level updated based on fund selection in Part A and expense ratio.
      
      dat$retirebenefit_worst[select_g4_q3_a] = if (select_g4_q3_a == 1) {
        dollar(update_retirebenefit_worst_g3_q3_fundb(), accuracy = 1)
      } else {
        dollar(update_retirebenefit_worst_g3_q3_fundc(), accuracy = 1)
      }
      
      dat$retirebenefit_expected[select_g4_q3_a] = if (select_g4_q3_a == 1) {
        dollar(update_retirebenefit_expected_g3_q3_fundb(), accuracy = 1)
      } else {
        dollar(update_retirebenefit_expected_g3_q3_fundc(), accuracy = 1)
      }
      
      dat$retirebenefit_best[select_g4_q3_a] = if (select_g4_q3_a == 1) {
        dollar(update_retirebenefit_best_g3_q3_fundb(), accuracy = 1)
      } else {
        dollar(update_retirebenefit_best_g3_q3_fundc(), accuracy = 1)
      }
      
      dat$retirebenefit_sd[select_g4_q3_a] = if (select_g4_q3_a == 1) {
        dollar(update_retirebenefit_sd_g3_q3_fundb(), accuracy = 1)
      } else {
        dollar(update_retirebenefit_sd_g3_q3_fundc(), accuracy = 1)
      }# Retirement benefit based on fund selection in Part A and expense ratio.
    }
    
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age)  </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  # Choices: table_g4_q3_c2 ---------------------------------------
  observeEvent(input$button_to_g4_q3_c, {
    if (isTruthy(input$choose_g4_q3_b)) {
      choices_g4_q3_c2 <- switch(input$choose_g4_q3_b,
                                 "Fund A" = c("Fund B", "Fund C"),
                                 "Fund B" = c("Fund A", "Fund C"),
                                 "Fund C" = c("Fund A", "Fund B"))
      updateRadioButtons(session, "choose_g4_q3_c2", choices = choices_g4_q3_c2, selected = character(0))
    }
  })
  
  # Table: table_g4_q4_a ---------------------------------------
  
  df_g4_q4_a1 <- reactive({
    
    specs <- get_first_4a()
    data.frame(
      fund_no = c("Fund A"),
      type_fund = c("Target Date Fund"),
      target_date = as.integer(c(year_retire())),
      gross_expratio = percent(c(0.002), accuracy = 0.01),
      current_e = percent(c(specs$f[1]), accuracy = 1),
      current_fi = percent(c(specs$fb[1]), accuracy = 1),
      middle_e = percent(c(specs$m[1]), accuracy = 1),
      middle_fi = percent(c(specs$mb[1]), accuracy = 1),
      final_e = percent(c(specs$l[1]), accuracy = 1),
      final_fi = percent(c(specs$lb[1]), accuracy = 1),
      retirebenefit_worst = dollar(specs$rbw[1], accuracy = 1),
      retirebenefit_expected = dollar(specs$rbe[1], accuracy = 1),
      retirebenefit_best = dollar(specs$rbb[1], accuracy = 1),
      retirebenefit_sd = dollar(specs$rbs[1], accuracy = 1)
    )})
  
  output$table_g4_q4_a1 <- renderTable({
    
    head(df_g4_q4_a1())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation  </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income"
  ), 
  align = 'c')
  
  
  df_g4_q4_a2 <- reactive({
    
    specs <- get_first_4b()
    data.frame(
      fund_no = c("Fund B"),
      type_fund = c("Target Date RILA"),
      target_date = as.integer(c(year_retire())),
      gross_expratio = percent(c(0.002), accuracy = 0.01),
      current_floor = percent(c(specs$f[1]), accuracy = 0.1),
      current_cap = percent(c(specs$fb[1]), accuracy = 0.1),
      middle_floor = percent(c(specs$m[1]), accuracy = 0.1),
      middle_cap = percent(c(specs$mb[1]), accuracy = 0.1),
      final_floor = percent(c(specs$l[1]), accuracy = 0.1),
      final_cap = percent(c(specs$lb[1]), accuracy = 0.1),
      retirebenefit_worst = dollar(specs$rbw[1], accuracy = 1),
      retirebenefit_expected = dollar(specs$rbe[1], accuracy = 1),
      retirebenefit_best = dollar(specs$rbb[1], accuracy = 1),
      retirebenefit_sd = dollar(specs$rbs[1], accuracy = 1)
    )})
  
  output$table_g4_q4_a2 <- renderTable({
    
    head(df_g4_q4_a2())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  df_g4_q4_a3 <- reactive({
    
    specs <- get_first_4c()
    data.frame(
      fund_no = c("Fund C"),
      type_fund = c("Target Date RILA"),
      target_date = as.integer(c(year_retire())),
      gross_expratio = percent(c(0.002), accuracy = 0.01),
      current_buffer = percent(c(specs$f[1]), accuracy = 0.1),
      current_cap = percent(c(specs$fb[1]), accuracy = 0.1),
      middle_buffer = percent(c(specs$m[1]), accuracy = 0.1),
      middle_cap = percent(c(specs$mb[1]), accuracy = 0.1),
      final_buffer = percent(c(specs$l[1]), accuracy = 0.1),
      final_cap = percent(c(specs$lb[1]), accuracy = 0.1),
      retirebenefit_worst = dollar(specs$rbw[1], accuracy = 1),
      retirebenefit_expected = dollar(specs$rbe[1], accuracy = 1),
      retirebenefit_best = dollar(specs$rbe[1], accuracy = 1),
      retirebenefit_sd = dollar(specs$rbs[1], accuracy = 1)
    )})
  
  output$table_g4_q4_a3 <- renderTable({
    
    
    head(df_g4_q4_a3())
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Table: table_g4_q4_b ---------------------------------------
  output$table_g4_q4_b1 <- renderTable({
    dat <- df_g4_q4_a1()
    if (input$choose_g4_q4_a == "Fund A") {
      dat$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01)# Gross Expense Ratio changes for fund selected in Part A.
      dat$retirebenefit_worst = dollar(update_retirebenefit_worst_g3_q1_funda(), accuracy = 1)
      dat$retirebenefit_expected = dollar(update_retirebenefit_expected_g3_q1_funda(), accuracy = 1)
      dat$retirebenefit_best = dollar(update_retirebenefit_best_g3_q1_funda(), accuracy = 1)
      dat$retirebenefit_sd = dollar(update_retirebenefit_sd_g3_q1_funda(), accuracy = 1)# Retirement benefit based on fund selection in Part A and expense ratio.
    }
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Target Allocation </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Fixed-Income </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Equity  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Fixed-Income"
  ), 
  align = 'c')
  
  output$table_g4_q4_b2 <- renderTable({
    dat <- df_g4_q4_a2()
    if (input$choose_g4_q4_a == "Fund B") {
      dat$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01)# Gross Expense Ratio changes for fund selected in Part A.
      dat$current_cap = percent(update_current_cap_g1_q2_funda(), accuracy = 0.1)
      dat$middle_cap = percent(update_middle_cap_g1_q2_funda(), accuracy = 0.1)
      dat$final_cap = percent(update_final_cap_g1_q2_funda(), accuracy = 0.1)# Cap level updated based on fund selection in Part A and expense ratio.
      dat$retirebenefit_worst = dollar(update_retirebenefit_worst_g3_q2_funda(), accuracy = 1)
      dat$retirebenefit_expected = dollar(update_retirebenefit_expected_g3_q2_funda(), accuracy = 1)
      dat$retirebenefit_best = dollar(update_retirebenefit_best_g3_q2_funda(), accuracy = 1)
      dat$retirebenefit_sd = dollar(update_retirebenefit_sd_g3_q2_funda(), accuracy = 1)# Retirement benefit based on fund selection in Part A and expense ratio.
    }
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Floor / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Floor  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  output$table_g4_q4_b3 <- renderTable({
    dat <- df_g4_q4_a3()
    if (input$choose_g4_q4_a == "Fund C") {
      dat$gross_expratio = percent(cs$Cost_grid[(gross_expense_ratio_random)], accuracy = 0.01)# Gross Expense Ratio changes for fund selected in Part A.
      dat$current_cap = percent(update_current_cap_g1_q3_funda(), accuracy = 0.1)
      dat$middle_cap = percent(update_middle_cap_g1_q3_funda(), accuracy = 0.1)
      dat$final_cap = percent(update_final_cap_g1_q3_funda(), accuracy = 0.1)# Cap level updated based on fund selection in Part A and expense ratio.
      dat$retirebenefit_worst = dollar(update_retirebenefit_worst_g3_q3_funda(), accuracy = 1)
      dat$retirebenefit_expected = dollar(update_retirebenefit_expected_g3_q3_funda(), accuracy = 1)
      dat$retirebenefit_best = dollar(update_retirebenefit_best_g3_q3_funda(), accuracy = 1)
      dat$retirebenefit_sd = dollar(update_retirebenefit_sd_g3_q3_funda(), accuracy = 1)# Retirement benefit based on fund selection in Part A and expense ratio. 
    }
    head(dat)
  },
  include.colnames = FALSE,
  add.to.row = list(pos = list(0),
                    command = "<style type='text/css'>
                               .tg-9wq8{border-color:inherit;text-align:center;vertical-align:middle}
                               .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
                               </style>
                               <thead>
                               <tr><th class='tg-9wq8' rowspan='3' > </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Type of Fund  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Target Date  </th><th class='tg-9wq8' rowspan='3' style='border-left: 1px solid #cdd0d4'> Implied Gross Expense Ratio   </th><th class='tg-c3ow' colspan='6' style='border-left: 1px solid #cdd0d4'> Buffer / Cap Level </th><th class='tg-c3ow' colspan='4' style='border-left: 1px solid #cdd0d4'> Anticipated Retirement Benefit (Monthly Income)*
                               <tr> </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Current  </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Middle </th><th class='tg-c3ow' colspan='2' style='border-left: 1px solid #cdd0d4'> Final (at retirement age) </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Worst Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Expected Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Best Probable Outcome </th><th class='tg-c3ow' rowspan='2' style='border-left: 1px solid #cdd0d4'> Standard Deviation
                               <tr> </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow' style='border-left: 1px solid #cdd0d4'> Cap </th><th class='tg-c3ow'style='border-left: 1px solid #cdd0d4'> Buffer  </th><th class='tg-c3ow'  style='border-left: 1px solid #cdd0d4'> Cap"
  ), 
  align = 'c')
  
  # Choices: table_g4_q4_c2 ---------------------------------------
  observeEvent(input$button_to_g4_q4_c, {
    if (isTruthy(input$choose_g4_q4_b)) {
      choices_g4_q4_c2 <- switch(input$choose_g4_q4_b,
                                 "Fund A" = c("Fund B", "Fund C"),
                                 "Fund B" = c("Fund A", "Fund C"),
                                 "Fund C" = c("Fund A", "Fund B"))
      updateRadioButtons(session, "choose_g4_q4_c2", choices = choices_g4_q4_c2, selected = character(0))
    }
  })
  
  
  # Table: background information ---------------------------------------
  df_backgroundinfo = matrix(nrow = 10, ncol = 2, byrow = TRUE,
                             dimnames = list(paste0(LETTERS[1:10], "13"), LETTERS[1:2])
  )
  
  v_backgroundinfo_a <- c("10% chance of $200 and 90% chance of $160",
                          "20% chance of $200 and 80% chance of $160",
                          "30% chance of $200 and 70% chance of $160",
                          "40% chance of $200 and 60% chance of $160",
                          "50% chance of $200 and 50% chance of $160",
                          "60% chance of $200 and 40% chance of $160",
                          "70% chance of $200 and 30% chance of $160",
                          "80% chance of $200 and 20% chance of $160",
                          "90% chance of $200 and 10% chance of $160",
                          "100% chance of $200 and 0% chance of $160")
  
  v_backgroundinfo_b <- c("10% chance of $385 and 90% chance of $10",
                          "20% chance of $385 and 80% chance of $10",
                          "30% chance of $385 and 70% chance of $10",
                          "40% chance of $385 and 60% chance of $10",
                          "50% chance of $385 and 50% chance of $10",
                          "60% chance of $385 and 40% chance of $10",
                          "70% chance of $385 and 30% chance of $10",
                          "80% chance of $385 and 20% chance of $10",
                          "90% chance of $385 and 10% chance of $10",
                          "100% chance of $385 and 0% chance of $10")
  
  df_backgroundinfo = cbind(v_backgroundinfo_a, v_backgroundinfo_b, df_backgroundinfo)
  
  colnames(df_backgroundinfo) = c("Option A", "Option B", "A", "B")
  
  output$table_backgroundinfo = DT::renderDataTable(
    df_backgroundinfo, escape = FALSE, selection = 'none', server = FALSE,
    options = list(dom = 't', paging = FALSE, ordering = FALSE),
    callback = JS("table.rows().every(function(i, tab, row) {
          var $this = $(this.node());
          $this.attr('id', this.data()[0]);
          $this.addClass('shiny-input-radiogroup');
        });
        Shiny.unbindAll(table.table().node());
        Shiny.bindAll(table.table().node());")
  )
  
  loss_aver <- reactive({
    dt$result <- sapply(unique(my_table$rowid), function(x) input[[x]])
    dtWithRadioButton$result <- dt
  })
  
  # Saving Data -------------------------------------------------------------
  save_mark <- 0
  output$download <- downloadHandler(
    filename = function(){"test.RData"},
    content = function(file) {
      save(input, file)
    }
  )
  
  attention <- 0
  
  
  # Function to get retire year ---------------------------------
  comp_code <- reactive(
    {
      if((input$choose_add_q23k == "0% chance of $385 and 100% chance of $10" |
         input$choose_add_q23j == "100% chance of $200 and 0% chance of $160") &
         (input$choose_add_q12a == "Gaining $2000 with probability 80% and gaining $0 with probability 20%" | 
         input$choose_add_q11a == "A sure loss of $1500")){ # Wrong on attention questions
        return("CMO2AZG6")
      } else {
        return("CRK5PXPV")
      }
    }
  )
  
  output$comp_code <- renderText(comp_code())
  
  observeEvent(input$button_to_last_page, {
    if(isTruthy(input$choose_add_q1) & 
       isTruthy(input$choose_add_q2) & 
       isTruthy(input$choose_add_q3) & 
       isTruthy(input$choose_add_q4) & 
       isTruthy(input$choose_add_q6) & 
       isTruthy(input$choose_add_q7) & 
       isTruthy(input$choose_add_q8) & 
       isTruthy(input$choose_add_q9) & 
       isTruthy(input$choose_add_q10) & 
       isTruthy(input$choose_add_q11) & 
       isTruthy(input$choose_add_q12) & 
       isTruthy(input$choose_add_q13) &  
       isTruthy(input$choose_add_q15) & 
       isTruthy(input$choose_add_q16) & 
       isTruthy(input$choose_add_q17) & 
       isTruthy(input$choose_add_q18) & 
       isTruthy(input$choose_add_q19) & 
       isTruthy(input$choose_add_q20) & 
       isTruthy(input$choose_add_q21) & 
       isTruthy(input$choose_add_q22) & 
       isTruthy(input$choose_add_q23a) & 
       isTruthy(input$choose_add_q23b) & 
       isTruthy(input$choose_add_q23c) & 
       isTruthy(input$choose_add_q23d) & 
       isTruthy(input$choose_add_q23e) & 
       isTruthy(input$choose_add_q23f) & 
       isTruthy(input$choose_add_q23g) & 
       isTruthy(input$choose_add_q23h) & 
       isTruthy(input$choose_add_q23i) & 
       isTruthy(input$choose_add_q23j) & 
       isTruthy(input$choose_add_q23k)){
      
      if(input$choose_add_q23k == "0% chance of $385 and 100% chance of $10" |
         input$choose_add_q23j == "100% chance of $200 and 0% chance of $160"){
        attention <- 1
      }
      
      # if(
      #   (input$g1_q1_c1 == .2 |
      #    is.null(input$g1_q1_c1)) &
      #   (input$g1_q1_c2 == cost_alt() |
      #    is.null(input$g1_q1_c2)) &
      #   (input$g1_q2_c1 == .2 |
      #    is.null(input$g1_q2_c1)) &
      #   (input$g1_q2_c2 == cost_alt() |
      #    is.null(input$g1_q2_c2)) &
      #   (input$g1_q3_c1 == .2 |
      #    is.null(input$g1_q3_c1)) &
      #   (input$g1_q3_c2 == cost_alt() |
      #    is.null(input$g1_q3_c2)) &
      #   (input$g1_q4_c1 == .2 |
      #    is.null(input$g1_q4_c1)) &
      #   (input$g1_q4_c2 == cost_alt() |
      #    is.null(input$g1_q4_c2))
      # ){
      #   attention <- 2
      # }
      
      if(group_assigned == 1) {
        full_data <- matrix(NA, 
                            ncol = 68,
                            nrow = 1) %>% data.frame()
        full_data[1,1] <- start_time
        full_data[1,2] <- Sys.time()
        full_data[1,3] <- group_assigned
        full_data[1,4] <- gross_expense_ratio_random
        full_data[1,5] <- f(input$age_current)
        full_data[1,6] <- f(input$age_retire)
        full_data[1,7] <- f(input$salary_current)
        full_data[1,8] <- f(input$retire_plan)
        full_data[1,9] <- f(input$choose_g1_q1_a)
        full_data[1,10] <- f(input$choose_g1_q1_b)
        full_data[1,11] <- f(input$g1_q1_c1)
        full_data[1,12] <- f(input$g1_q1_c2)
        full_data[1,13] <- f(input$choose_g1_q1_c)
        full_data[1,14] <- f(input$choose_g1_q2_a)
        full_data[1,15] <- f(input$choose_g1_q2_b)
        full_data[1,16] <- f(input$g1_q2_c1)
        full_data[1,17] <- f(input$g1_q2_c2)
        full_data[1,18] <- f(input$choose_g1_q2_c)
        full_data[1,19] <- f(input$choose_g1_q3_a)
        full_data[1,20] <- f(input$choose_g1_q3_b)
        full_data[1,21] <- f(input$g1_q3_c1)
        full_data[1,22] <- f(input$g1_q3_c2)
        full_data[1,23] <- f(input$choose_g1_q3_c)
        full_data[1,24] <- f(input$choose_g1_q4_a)
        full_data[1,25] <- f(input$choose_g1_q4_b)
        full_data[1,26] <- f(input$g1_q4_c1)
        full_data[1,27] <- f(input$g1_q4_c2)
        full_data[1,28] <- f(input$choose_g1_q4_c)
        full_data[1,29] <- f(input$choose_add_q1)
        full_data[1,30] <- f(input$choose_add_q2)
        full_data[1,31] <- f(input$choose_add_q3)
        full_data[1,32] <- f(input$choose_add_q4)
        full_data[1,33] <- 0
        full_data[1,34] <- f(input$choose_add_q6)
        full_data[1,35] <- f(input$choose_add_q7)
        full_data[1,36] <- f(input$choose_add_q8)
        full_data[1,37] <- f(input$choose_add_q9)
        full_data[1,38] <- f(input$choose_add_q10)
        full_data[1,39] <- f(input$choose_add_q11)
        full_data[1,40] <- f(input$choose_add_q12)
        full_data[1,41] <- f(input$choose_add_q13)
        full_data[1,42] <- f(input$choose_add_q15)
        full_data[1,43] <- f(input$choose_add_q16)
        full_data[1,44] <- f(input$choose_add_q17)
        full_data[1,45] <- f(input$choose_add_q18)
        full_data[1,46] <- f(input$choose_add_q19)
        full_data[1,47] <- f(input$choose_add_q20)
        full_data[1,48] <- f(input$choose_add_q21)
        full_data[1,49] <- f(input$choose_add_q22)
        full_data[1,50] <- f(input$choose_practice_q1)
        full_data[1,51] <- f(input$choose_practice_q3_1)
        full_data[1,52] <- f(input$choose_practice_q3_2)
        full_data[1,53] <- f(input$choose_practice_q3_3)
        full_data[1,54] <- f(input$choose_practice_q3_4)
        full_data[1,55] <- f(input$choose_practice_q4)
        full_data[1,56] <- f(input$choose_practice_q5)
        full_data[1,57] <- f(input$worker_ID)
        full_data[1,58] <- f(input$choose_add_q23a)
        full_data[1,59] <- f(input$choose_add_q23b)
        full_data[1,60] <- f(input$choose_add_q23c)
        full_data[1,61] <- f(input$choose_add_q23d)
        full_data[1,62] <- f(input$choose_add_q23e)
        full_data[1,63] <- f(input$choose_add_q23f)
        full_data[1,64] <- f(input$choose_add_q23g)
        full_data[1,65] <- f(input$choose_add_q23h)
        full_data[1,66] <- f(input$choose_add_q23i)
        full_data[1,67] <- f(input$choose_add_q23j)
        full_data[1,68] <- f(input$choose_add_q23k)
        
      }
      
      if(group_assigned == 2){
        full_data <- matrix(NA, 
                            ncol = 68,
                            nrow = 1) %>% data.frame()
        full_data[1,1] <- start_time
        full_data[1,2] <- Sys.time()
        full_data[1,3] <- group_assigned
        full_data[1,4] <- gross_expense_ratio_random
        full_data[1,5] <- f(input$age_current)
        full_data[1,6] <- f(input$age_retire)
        full_data[1,7] <- f(input$salary_current)
        full_data[1,8] <- f(input$retire_plan)
        full_data[1,9] <- f(input$choose_g2_q1_a)
        full_data[1,10] <- f(input$choose_g2_q1_b)
        full_data[1,11] <- f(input$g2_q1_c1)
        full_data[1,12] <- f(input$g2_q1_c2)
        full_data[1,13] <- f(input$choose_g2_q1_c)
        full_data[1,14] <- f(input$choose_g2_q2_a)
        full_data[1,15] <- f(input$choose_g2_q2_b)
        full_data[1,16] <- f(input$g2_q2_c1)
        full_data[1,17] <- f(input$g2_q2_c2)
        full_data[1,18] <- f(input$choose_g2_q2_c)
        full_data[1,19] <- f(input$choose_g2_q3_a)
        full_data[1,20] <- f(input$choose_g2_q3_b)
        full_data[1,21] <- f(input$g2_q3_c1)
        full_data[1,22] <- f(input$g2_q3_c2)
        full_data[1,23] <- f(input$choose_g2_q3_c)
        full_data[1,24] <- f(input$choose_g2_q4_a)
        full_data[1,25] <- f(input$choose_g2_q4_b)
        full_data[1,26] <- f(input$g2_q4_c1)
        full_data[1,27] <- f(input$g2_q4_c2)
        full_data[1,28] <- f(input$choose_g2_q4_c)
        full_data[1,29] <- f(input$choose_add_q1)
        full_data[1,30] <- f(input$choose_add_q2)
        full_data[1,31] <- f(input$choose_add_q3)
        full_data[1,32] <- f(input$choose_add_q4)
        full_data[1,33] <- 0
        full_data[1,34] <- f(input$choose_add_q6)
        full_data[1,35] <- f(input$choose_add_q7)
        full_data[1,36] <- f(input$choose_add_q8)
        full_data[1,37] <- f(input$choose_add_q9)
        full_data[1,38] <- f(input$choose_add_q10)
        full_data[1,39] <- f(input$choose_add_q11)
        full_data[1,40] <- f(input$choose_add_q12)
        full_data[1,41] <- f(input$choose_add_q13)
        full_data[1,42] <- f(input$choose_add_q15)
        full_data[1,43] <- f(input$choose_add_q16)
        full_data[1,44] <- f(input$choose_add_q17)
        full_data[1,45] <- f(input$choose_add_q18)
        full_data[1,46] <- f(input$choose_add_q19)
        full_data[1,47] <- f(input$choose_add_q20)
        full_data[1,48] <- f(input$choose_add_q21)
        full_data[1,49] <- f(input$choose_add_q22)
        full_data[1,50] <- f(input$choose_practice_q1)
        full_data[1,51] <- f(input$choose_practice_q3_1)
        full_data[1,52] <- f(input$choose_practice_q3_2)
        full_data[1,53] <- f(input$choose_practice_q3_3)
        full_data[1,54] <- f(input$choose_practice_q3_4)
        full_data[1,55] <- f(input$choose_practice_q4)
        full_data[1,56] <- f(input$choose_practice_q5)
        full_data[1,57] <- f(input$worker_ID)
        full_data[1,58] <- f(input$choose_add_q23a)
        full_data[1,59] <- f(input$choose_add_q23b)
        full_data[1,60] <- f(input$choose_add_q23c)
        full_data[1,61] <- f(input$choose_add_q23d)
        full_data[1,62] <- f(input$choose_add_q23e)
        full_data[1,63] <- f(input$choose_add_q23f)
        full_data[1,64] <- f(input$choose_add_q23g)
        full_data[1,65] <- f(input$choose_add_q23h)
        full_data[1,66] <- f(input$choose_add_q23i)
        full_data[1,67] <- f(input$choose_add_q23j)
        full_data[1,68] <- f(input$choose_add_q23k)
      } 
      
      if(group_assigned == 3){
        full_data <- matrix(NA, 
                            ncol = 68,
                            nrow = 1) %>% data.frame()
        full_data[1,1] <- start_time
        full_data[1,2] <- Sys.time()
        full_data[1,3] <- group_assigned
        full_data[1,4] <- gross_expense_ratio_random
        full_data[1,5] <- f(input$age_current)
        full_data[1,6] <- f(input$age_retire)
        full_data[1,7] <- f(input$salary_current)
        full_data[1,8] <- f(input$retire_plan)
        full_data[1,9] <- f(input$choose_g3_q1_a)
        full_data[1,10] <- f(input$choose_g3_q1_b)
        full_data[1,11] <- f(input$g3_q1_c1)
        full_data[1,12] <- f(input$g3_q1_c2)
        full_data[1,13] <- f(input$choose_g3_q1_c)
        full_data[1,14] <- f(input$choose_g3_q2_a)
        full_data[1,15] <- f(input$choose_g3_q2_b)
        full_data[1,16] <- f(input$g3_q2_c1)
        full_data[1,17] <- f(input$g3_q2_c2)
        full_data[1,18] <- f(input$choose_g3_q2_c)
        full_data[1,19] <- f(input$choose_g3_q3_a)
        full_data[1,20] <- f(input$choose_g3_q3_b)
        full_data[1,21] <- f(input$g3_q3_c1)
        full_data[1,22] <- f(input$g3_q3_c2)
        full_data[1,23] <- f(input$choose_g3_q3_c)
        full_data[1,24] <- f(input$choose_g3_q4_a)
        full_data[1,25] <- f(input$choose_g3_q4_b)
        full_data[1,26] <- f(input$g3_q4_c1)
        full_data[1,27] <- f(input$g3_q4_c2)
        full_data[1,28] <- f(input$choose_g3_q4_c)
        full_data[1,29] <- f(input$choose_add_q1)
        full_data[1,30] <- f(input$choose_add_q2)
        full_data[1,31] <- f(input$choose_add_q3)
        full_data[1,32] <- f(input$choose_add_q4)
        full_data[1,33] <- 0
        full_data[1,34] <- f(input$choose_add_q6)
        full_data[1,35] <- f(input$choose_add_q7)
        full_data[1,36] <- f(input$choose_add_q8)
        full_data[1,37] <- f(input$choose_add_q9)
        full_data[1,38] <- f(input$choose_add_q10)
        full_data[1,39] <- f(input$choose_add_q11)
        full_data[1,40] <- f(input$choose_add_q12)
        full_data[1,41] <- f(input$choose_add_q13)
        full_data[1,42] <- f(input$choose_add_q15)
        full_data[1,43] <- f(input$choose_add_q16)
        full_data[1,44] <- f(input$choose_add_q17)
        full_data[1,45] <- f(input$choose_add_q18)
        full_data[1,46] <- f(input$choose_add_q19)
        full_data[1,47] <- f(input$choose_add_q20)
        full_data[1,48] <- f(input$choose_add_q21)
        full_data[1,49] <- f(input$choose_add_q22)
        full_data[1,50] <- f(input$choose_practice_q1)
        full_data[1,51] <- f(input$choose_practice_q3_1)
        full_data[1,52] <- f(input$choose_practice_q3_2)
        full_data[1,53] <- f(input$choose_practice_q3_3)
        full_data[1,54] <- f(input$choose_practice_q3_4)
        full_data[1,55] <- f(input$choose_practice_q4)
        full_data[1,56] <- f(input$choose_practice_q5)
        full_data[1,57] <- f(input$worker_ID)
        full_data[1,58] <- f(input$choose_add_q23a)
        full_data[1,59] <- f(input$choose_add_q23b)
        full_data[1,60] <- f(input$choose_add_q23c)
        full_data[1,61] <- f(input$choose_add_q23d)
        full_data[1,62] <- f(input$choose_add_q23e)
        full_data[1,63] <- f(input$choose_add_q23f)
        full_data[1,64] <- f(input$choose_add_q23g)
        full_data[1,65] <- f(input$choose_add_q23h)
        full_data[1,66] <- f(input$choose_add_q23i)
        full_data[1,67] <- f(input$choose_add_q23j)
        full_data[1,68] <- f(input$choose_add_q23k)
      } 
      
      if(group_assigned == 4){
        full_data <- matrix(NA, 
                            ncol = 68,
                            nrow = 1) %>% data.frame()
        full_data[1,1] <- start_time
        full_data[1,2] <- Sys.time()
        full_data[1,3] <- group_assigned
        full_data[1,4] <- gross_expense_ratio_random
        full_data[1,5] <- f(input$age_current)
        full_data[1,6] <- f(input$age_retire)
        full_data[1,7] <- f(input$salary_current)
        full_data[1,8] <- f(input$retire_plan)
        full_data[1,9] <- f(input$choose_g4_q1_a)
        full_data[1,10] <- f(input$choose_g4_q1_b)
        full_data[1,11] <- f(input$g4_q1_c1)
        full_data[1,12] <- f(input$g4_q1_c2)
        full_data[1,13] <- f(input$choose_g4_q1_c)
        full_data[1,14] <- f(input$choose_g4_q2_a)
        full_data[1,15] <- f(input$choose_g4_q2_b)
        full_data[1,16] <- f(input$g4_q2_c1)
        full_data[1,17] <- f(input$g4_q2_c2)
        full_data[1,18] <- f(input$choose_g4_q2_c)
        full_data[1,19] <- f(input$choose_g4_q3_a)
        full_data[1,20] <- f(input$choose_g4_q3_b)
        full_data[1,21] <- f(input$g4_q3_c1)
        full_data[1,22] <- f(input$g4_q3_c2)
        full_data[1,23] <- f(input$choose_g4_q3_c)
        full_data[1,24] <- f(input$choose_g4_q4_a)
        full_data[1,25] <- f(input$choose_g4_q4_b)
        full_data[1,26] <- f(input$g4_q4_c1)
        full_data[1,27] <- f(input$g4_q4_c2)
        full_data[1,28] <- f(input$choose_g4_q4_c)
        full_data[1,29] <- f(input$choose_add_q1)
        full_data[1,30] <- f(input$choose_add_q2)
        full_data[1,31] <- f(input$choose_add_q3)
        full_data[1,32] <- f(input$choose_add_q4)
        full_data[1,33] <- 0
        full_data[1,34] <- f(input$choose_add_q6)
        full_data[1,35] <- f(input$choose_add_q7)
        full_data[1,36] <- f(input$choose_add_q8)
        full_data[1,37] <- f(input$choose_add_q9)
        full_data[1,38] <- f(input$choose_add_q10)
        full_data[1,39] <- f(input$choose_add_q11)
        full_data[1,40] <- f(input$choose_add_q12)
        full_data[1,41] <- f(input$choose_add_q13)
        full_data[1,42] <- f(input$choose_add_q15)
        full_data[1,43] <- f(input$choose_add_q16)
        full_data[1,44] <- f(input$choose_add_q17)
        full_data[1,45] <- f(input$choose_add_q18)
        full_data[1,46] <- f(input$choose_add_q19)
        full_data[1,47] <- f(input$choose_add_q20)
        full_data[1,48] <- f(input$choose_add_q21)
        full_data[1,49] <- f(input$choose_add_q22)
        full_data[1,50] <- f(input$choose_practice_q1)
        full_data[1,51] <- f(input$choose_practice_q3_1)
        full_data[1,52] <- f(input$choose_practice_q3_2)
        full_data[1,53] <- f(input$choose_practice_q3_3)
        full_data[1,54] <- f(input$choose_practice_q3_4)
        full_data[1,55] <- f(input$choose_practice_q4)
        full_data[1,56] <- f(input$choose_practice_q5)
        full_data[1,57] <- f(input$worker_ID)
        full_data[1,58] <- f(input$choose_add_q23a)
        full_data[1,59] <- f(input$choose_add_q23b)
        full_data[1,60] <- f(input$choose_add_q23c)
        full_data[1,61] <- f(input$choose_add_q23d)
        full_data[1,62] <- f(input$choose_add_q23e)
        full_data[1,63] <- f(input$choose_add_q23f)
        full_data[1,64] <- f(input$choose_add_q23g)
        full_data[1,65] <- f(input$choose_add_q23h)
        full_data[1,66] <- f(input$choose_add_q23i)
        full_data[1,67] <- f(input$choose_add_q23j)
        full_data[1,68] <- f(input$choose_add_q23k)
      } 
      if(save_mark == 0){
        sheet_append(ss = sheet_id, sheet = "Data", data = full_data)
        save_mark <- 1
      }
    }
  })
  
}


shinyApp(ui, server)

